本文介绍了如何从两个列表中删除重复记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 类 - >Class-->public class DocumentsLists { public int DocumentId { get; set; } public string DocumentName { get; set; } } 列表 - >List-->var ChkDocumentData = new List<documentslists>();var AppDocData = new List<documentslists>(); 两个列表的内容 - & gt; ChkDocumentData包含:Contents of two list--> ChkDocumentData contains: { {DocumentId = 42, DocumentName ="Passport"}, {DocumentId = 44, DocumentName ="Adhar Card"}, {DocumentId = 45, DocumentName ="Driving License"}, {DocumentId = 46, DocumentName ="Voter ICard"}}; AppDocData包含: AppDocData contains :{DocumentId = 44, DocumentName ="Adhar Card"}; 我想要一个包含的列表: I want a list Contains :{ {DocumentId = 42, DocumentName ="Passport"}, {DocumentId = 45, DocumentName ="Driving License"}, {DocumentId = 46, DocumentName ="Voter ICard"}}; 提前致谢 Thanks in advance推荐答案看到: https://msdn.microsoft.com/en-us/library /vstudio/bb300779(v=vs.100).aspx [ ^ ] 另一种方式See this : https://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx[^]Another wayvar result = ChkDocumentData.Where(x => !AppDocData.Any(y => x.DocumentId == y.DocumentId)); 参考: http:/ /stackoverflow.com/a/5636511 [ ^ ] 问候..Reference : http://stackoverflow.com/a/5636511[^]Regards..试试这个,Try this,var result = ChkDocumentData.Where(c => !AppDocData.Any(a => a.DocumentId == c.DocumentId)).ToList(); -KR-KR作为 Rohan Leuva [ ^ ],最简单的方法是使用除方法:As Rohan Leuva[^] mentioned, the simplest way is to use Except method:var resultList = ChkDocumentData.Except(AppDocData); Sergey Alexandrovich Kryukov [ ^ ]建议在问题的评论中,你可以使用包含 [ ^ ]在向列表添加数据的过程中避免重复的方法。As Sergey Alexandrovich Kryukov[^] suggests in the comment to the question, you can use Contains[^] method to avoid duplicates during the process of adding data to the list.if(!ChkDocumentData.Contains(SomeDocument)){ ChkDocumentData.Add(SomeDocument);} 但最好的方法是创建自定义类集合并提供可以处理此类功能的Add方法(避免一式两份)。请参阅:演练:创建自己的收藏类 [ ^ ]But the best way is to create custom class collection and provide Add method which can handle such of functionality (avoiding duplicates). See: Walkthrough: Creating Your Own Collection Class[^]public class DocumentsCollection : CollectionBase{ public void Add(DocumentList dl) { if (!List.Contains(dl)) { List.Add(dl); } }} 这篇关于如何从两个列表中删除重复记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 00:16