我有两个通用列表,一个名为“Featured”,另一个名为“Filtered”。比较两个通用列表并删除重复项
List<Content> Featured = new List<Content>();
List<Content> Filtered = new List<Content>();
两者都包含“内容”的项目这是一个简单的类,像这样:
public class Content
{
public long ContentID { get; set;}
public string Title { get; set; }
public string Url { get; set; }
public string Image { get; set; }
public string Teaser { get; set; }
public Content(long contentId, string title, string url, string image, string teaser)
{
ContentID = contentId;
Title = title;
Url = url;
Image = image;
}
}
出现在“过滤”的任何项目,但也出现在“特色”需要从“过滤”删除。此外,这两个列表将被合并为一个通用列表,其中“精选”项目首先出现。
我知道我可以写几个foreach循环来做到这一点,但我不禁觉得必须有一个更优雅的方法使用LINQ。
我正在使用C#4.0。
因此,'精选'应该出现在整体,然后'过滤',但从'精选'的任何项目被删除? – 2012-07-09 13:08:09
是的,这是正确的:) – 2012-07-09 13:08:44
看看:http://stackoverflow.com/a/2060093/100283。它不是Linq,但它是比较两个对象的简单方法。 – 2012-07-09 13:09:12