请原谅我的混淆标题。但我不能想出更简单的东西。LINQ除/仅基于几列,不添加重复
我已经下课了。
public class Foo
{
public FooId int { get; set; }
public FooField string { get; set; }
//some more fields here....
//....
//....
public DateTime CreatedDate { get; set; }
}
我需要遍历的东西,并添加了一系列Foo
到List<Foo>
,但它不应该被重复的基础上,FooId
和FooField
组合。
所以我想,如下
List<Foo> foos = new List<Foo>();
foreach (Bar item in blah.Bars)
{
//Some code here to get the foos from the item
List<Foo> processedFoos = item.GetFoos();
//The problem is with below line
foos.AddRange(processedFoos.Except(foos));
}
的Except
增加了所有记录,并复制了FooId
和FooField
组合CreatedDate
将是所有独特记录。
但我需要忽略CreatedDate
,只是添加那些不违反独特组合的记录。
我能在这里做什么? Except
? Distinct
?任何其他的选择?
最重要的是,如何?
实施'的IEqualityComparer'和使用的适当的过载的'除()'的https:// MSDN ()。Select()().microsoft.com/zh-cn/usr/vstudio/bb336390(v = vs.100).aspx?cs-save-lang = 1&cs-lang = csharp#code-snippet-1 –
Ric