![](/img/trans.png)
[英]Can I get encrypt and decrypt algorithm that's more varied than what I have?
[英]how can i get distinct when I have more than 2 columns?
在我的實體中,如果我想要所有帶有不同D的列,那么我有4列A,B,C,D列?如何使用Linq
做到這一點? 例如:
Col A =1 Col B=2 Col C=3 Col D=4
Col A =1 Col B=2 Col C=9 Col D=4
現在,當我在Col D上做不同的記錄時,我想顯示其中一個記錄而不是全部顯示。 有沒有辦法在Linq
做到這一點,我不想使用循環語句來做到這一點。
void Main()
{
//Your provided sample data
IEnumerable<Item> data = new Item[]
{
new Item{ A = 1, B = 2, C = 3, D = 4 },
new Item{ A = 1, B = 2, C = 9, D = 4 },
};
//Create a custom comparer for distinct'ing
CustomComparer comparer = new CustomComparer();
//Use the overload for distinct
IEnumerable<Item> distinctData = data.Distinct(comparer);
//Now we have a distinct list according to your comparer
foreach (var element in distinctData)
{
Console.WriteLine(element.C.ToString()); // => 3
}
}
//sample class that holds your data
class Item
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
}
class CustomComparer : IEqualityComparer<Item>
{
public bool Equals(Item x, Item y)
{
return x.D == y.D;
}
public int GetHashCode(Item obj)
{
return obj.D;
}
}
您確定不想編寫該簡單循環:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.