从 Linq 的数据表中选择不同的行
问题描述:
我正在尝试根据多列(attribute1_name、attribute2_name)获取不同的行,并使用 Linq-to-Dataset 从数据表中获取数据行.
I am trying to get distinct rows based on multiple columns (attribute1_name, attribute2_name) and get datarows from datatable using Linq-to-Dataset.
我想要这样的结果
attribute1_name attribute2_name
-------------- ---------------
Age State
Age weekend_percent
Age statebreaklaw
Age Annual Sales
Age Assortment
如何精简 Linq-to-dataset?
How to do thin Linq-to-dataset?
答
如果它不是类型化数据集,那么您可能想要使用 Linq-to-DataSet 扩展方法来做这样的事情:
If it's not a typed dataset, then you probably want to do something like this, using the Linq-to-DataSet extension methods:
var distinctValues = dsValues.AsEnumerable()
.Select(row => new {
attribute1_name = row.Field<string>("attribute1_name"),
attribute2_name = row.Field<string>("attribute2_name")
})
.Distinct();
确保在代码的开头有一个 using System.Data;
语句,以启用 Linq-to-Dataset 扩展方法.
Make sure you have a using System.Data;
statement at the beginning of your code in order to enable the Linq-to-Dataset extension methods.
希望这有帮助!