如何在C#中的两个列表中获得差异?

好的,所以我在C#中有两个列表

List attributes = new List(); List songs = new List(); 

一个是字符串,一个是我创建的属性对象。非常简单

 class Attribute { public string size { get; set; } public string link { get; set; } public string name { get; set; } public Attribute(){} public Attribute(string s, string l, string n) { size = s; link = l; name = n; } } 

我现在必须进行比较,以查看属性名称中没有的歌曲,例如

 songs.Add("something"); songs.Add("another"); songs.Add("yet another"); Attribute a = new Attribute("500", "http://google.com", "something" ); attributes.Add(a); 

我想要一种方法来返回“另一个”和“另一个”,因为它们不在属性列表名称中

所以对于伪代码

 difference = songs - attributes.names 

 var difference = songs.Except(attributes.Select(s=>s.name)).ToList(); 

编辑

添加了ToList()以使其成为列表

值得指出的是,此处发布的答案将返回attributes.names不存在的songs列表,但它不会为您提供songs不存在的attributes.names列表。

虽然这是OP想要的,但标题可能有点误导,特别是如果(像我一样)你来这里寻找一种方法来检查两个列表的内容是否有所不同。 如果这是你想要的,你可以使用以下内容: –

 var differences = new HashSet(songs); differences.SymmetricExceptWith(attributes.Select(a => a.name)); if (differences.Any()) { // The lists differ. } 
 var diff = songs.Except(attributes.Select(a => a.name)).ToList(); 

这是查找属性名称中未包含的所有歌曲的方法:

 var result = songs .Where(!attributes.Select(a => a.name).ToList().Contains(song)); 

使用Except的答案也很完美,可能更有效。

编辑:如果你在LINQ to SQL中使用它,这个sintax有一个优点:它转换为NOT IN SQL谓词。 Except在SQL中没有翻译成任何东西。 因此,在该上下文中,所有记录都将从数据库中恢复,并且在应用程序端除外,效率低得多。