如何删除两个列表中的重复记录! [英] How To remove A Duplicate Record Withen Two List !

查看:77
本文介绍了如何删除两个列表中的重复记录!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public ActionResult GetDocument(int? DocumentTypeId)
        {
            int id = (int)Session["ApplicationId"];
            var DropDataChild = new List<documentslists>();
            var ChkDocumentData = new List<documentslists>();
            var AppDocData = new List<documentslists>();
            int chk = 0;

            ChkDocumentData = (from a in dbContext.SysDocuments
                               join b in dbContext.SysDocumentTypes
                               on a.DocumentTypeId equals DocumentTypeId
                               select new DocumentsLists
                               {
                                   DocumentId = a.DocumentId,
                                   DocumentName = a.DocumentName
                               }).Distinct().ToList();
            foreach (var z in ChkDocumentData)
            {
                AppDocData = (from c in dbContext.KenPartnerAppDocuments
                              join d in dbContext.SysDocuments on c.DocumentId equals d.DocumentId
                              where c.ApplicationId == id && c.DocumentId == z.DocumentId
                              select new DocumentsLists
                              {
                                  DocumentId = d.DocumentId,
                                  DocumentName = d.DocumentName
                              }).ToList();
                if (AppDocData.Count > 0)
                {
                    chk++;
                   
                }
            }
            if (chk > 0)
            {
               DropDataChild = ChkDocumentData.Except(AppDocData).ToList();
               
            }

          

            
            return Json(DropDataChild, JsonRequestBehavior.AllowGet);
        }

推荐答案

尝试使用以下代码:

Try with below code:
var ChkDocumentData = new List<documentslists>()
{
	new DocumentsLists {DocumentId = 42, DocumentName ="Passport"},
	new DocumentsLists {DocumentId = 44, DocumentName ="Adhar Card"},
	new DocumentsLists {DocumentId = 45, DocumentName ="Driving License"},
	new DocumentsLists {DocumentId = 46, DocumentName ="Voter ICard"}
};

var AppDocData = new List<documentslists>()
 {
	new DocumentsLists {DocumentId = 44, DocumentName ="Adhar Card"}
};

List<documentslists> tempList = ChkDocumentData.Concat(AppDocData).ToList<documentslists>();

var idList = tempList.Select(x => new { x.DocumentId, x.DocumentName }).Distinct();
foreach(var temp in idList)
{
   int DocumentId =  temp.DocumentId; 
   string DocumentId = temp.DocumentName;
}



你可以采用不同的方式(distinctby),这是一个解决方案。


You can do it different ways(distinctby), here is one solution.


另一种获取方式两个列表中的不同项目如下所示



Another way of getting distinct items from two lists is as below

List<testclass> L1 = new List<testclass>();
           List<testclass> L2 = new List<testclass>();
           L1.Add(new testClass() { ID = 4, Name = "dfghdfhdfh" });
           L1.Add(new testClass() { ID = 5, Name = "d6fghdfhdfh" });
           L1.Add(new testClass() { ID = 6, Name = "5dfghdfhdfh" });
           L2.Add(new testClass() { ID = 4, Name = "dfghdfhdfh" });
           L1.Add(new testClass() { ID = 41, Name = "dfghdfhdfh" });
           L2.Add(new testClass() { ID = 15, Name = "1d6fghdfhdfh" });

           var x1 =
                (from msg in L1
                where !L2.Any(x => x.ID == msg.ID && x.Name == msg.Name)
                select msg)
                .Concat(from msg in L2
                        where !L1.Any(x => x.ID == msg.ID && x.Name == msg.Name)
                        select msg)
                ;


这篇关于如何删除两个列表中的重复记录!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆