[英]Getting distinct objects from a list
Edit: The problem is not with distinct method but how Title is coded.编辑:问题不在于不同的方法,而在于标题的编码方式。 Comparising with StringComparison.InvariantCultureIgnoreCase retruns true.
与 StringComparison.InvariantCultureIgnoreCase 比较返回 true。 Problem closed.
问题关闭。 Here is my code:
这是我的代码:
var b = ret.DistinctBy(x => new { x.Title, x.Type }).ToList();
When I run this line with my input I get this.当我用我的输入运行这条线时,我得到了这个。 I want to eliminate duplicates based on Title and Type.
我想根据标题和类型消除重复项。 Can you tell me where I make a mistake?
你能告诉我我哪里出错了吗? Aren't these 2 objects the same based on my comparison?
根据我的比较,这两个对象不是相同的吗?
Thanks谢谢
Edit: Did more debugging.编辑:做了更多的调试。 It appears that the names are different.
名字好像不一样。
var z = ret[0].Title == ret[1].Title;
Checked with text comparer.用文本比较器检查。 It is the same.
这是相同的。 Any ideas why?
任何想法为什么? I read MyClippings from Kindle.
我从 Kindle 阅读了 MyClippings。 Will test if they code Title differently.
将测试他们是否以不同的方式对 Title 进行编码。
Can you share a little more code ie the Class using my example below it works correctly:您能否分享更多代码,即使用下面我的示例的类可以正常工作:
public class Test {
public string Title {get;set;}
public Type Type {get;set;}
}
public enum Type {
Kindle
}
public static void Main()
{
var ret = new List<Test> {
new Test {
Title = "Book A",
Type = Type.Kindle
},
new Test {
Title = "Book A",
Type = Type.Kindle
}
};
var b = ret.DistinctBy(x => new { x.Title, x.Type }).ToList();
b.ForEach(x => Console.WriteLine(x.Title));
}
Outputs:输出:
Book A
Runnable Version : https://dotnetfiddle.net/gi8z7j可运行版本: https : //dotnetfiddle.net/gi8z7j
If you change the second Book A
to Book B
it outputs:如果您将第二
Book A
更改为Book B
它将输出:
Book A
Book B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.