[英]Select distinct in Linq query
I've a collection with the data like this. 我有一个像这样的数据集合。
--------------------------------------------
| key | customer Name | code | isActive |
--------------------------------------------
| 30002 | XYZ | 234 | 1 |
--------------------------------------------
| 30002 | XYZ | 234 | 1 |
--------------------------------------------
| 30002 | XYZ | 234 | 1 |
--------------------------------------------
| 30034 | ERR | 344 | 1 |
--------------------------------------------
| 30031 | LDD | 343 | 1 |
--------------------------------------------
how can select the distinct data using linq? 如何使用linq选择不同的数据?
明显的队长说,您应该使用Distinct
linq方法:-)
You can create your own comparier like this: 您可以这样创建自己的比较器:
public class MyComparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y) {
return x["col1"] == y.["col1"] && x["col2"] == y.["col2"];
}
public int GetHashCode(DataRow obj) {
return obj["col1"].GetHashCode() ^ obj["col2"].GetHashCode();
}
}
Then use: 然后使用:
var distinctRows = (from dr in table.AsEnumerable()
select dr).Distinct(new MyComparer());
I think that code can be optimized, but the overall idea is presented :) 我认为可以优化代码,但是提出了总体思路:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.