Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过使用linq或反射比较2个列表来获得更新或修改的列表记录 ListofNewClient=新列表{ 新建客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的新客户端{ClientID=101,ClientName=“Rahul”,companyLocation=“USA”,companyName=“TATA”} }; List ListofOldClient=新列表{ 新的旧客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的旧客户端{ClientID=3,ClientName=“Mary”,companyLocation=“UK”,companyName=“KG”}, 新的旧客户端{ClientID=7,ClientName=“jhon”,companyLocation=“USA”,companyName=“TG”}, 新的旧客户端{ClientID=9,ClientName=“Tom”,companyLocation=“India”,companyName=“PG”} }; var Common=ListofNewClient.Where(n=>ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList(); var Deleted=listofooldclient.Where(o=>!ListofNewClient.Any(n=>n.ClientID==o.ClientID)).ToList(); var NewlyAdded=ListofNewClient.Where(n=>!ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList();_C#_Linq_Reflection - Fatal编程技术网

C# 如何通过使用linq或反射比较2个列表来获得更新或修改的列表记录 ListofNewClient=新列表{ 新建客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的新客户端{ClientID=101,ClientName=“Rahul”,companyLocation=“USA”,companyName=“TATA”} }; List ListofOldClient=新列表{ 新的旧客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的旧客户端{ClientID=3,ClientName=“Mary”,companyLocation=“UK”,companyName=“KG”}, 新的旧客户端{ClientID=7,ClientName=“jhon”,companyLocation=“USA”,companyName=“TG”}, 新的旧客户端{ClientID=9,ClientName=“Tom”,companyLocation=“India”,companyName=“PG”} }; var Common=ListofNewClient.Where(n=>ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList(); var Deleted=listofooldclient.Where(o=>!ListofNewClient.Any(n=>n.ClientID==o.ClientID)).ToList(); var NewlyAdded=ListofNewClient.Where(n=>!ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList();

C# 如何通过使用linq或反射比较2个列表来获得更新或修改的列表记录 ListofNewClient=新列表{ 新建客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的新客户端{ClientID=101,ClientName=“Rahul”,companyLocation=“USA”,companyName=“TATA”} }; List ListofOldClient=新列表{ 新的旧客户端{ClientID=1,ClientName=“Nilesh”,companyLocation=“孟买”,companyName=“CG”}, 新的旧客户端{ClientID=3,ClientName=“Mary”,companyLocation=“UK”,companyName=“KG”}, 新的旧客户端{ClientID=7,ClientName=“jhon”,companyLocation=“USA”,companyName=“TG”}, 新的旧客户端{ClientID=9,ClientName=“Tom”,companyLocation=“India”,companyName=“PG”} }; var Common=ListofNewClient.Where(n=>ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList(); var Deleted=listofooldclient.Where(o=>!ListofNewClient.Any(n=>n.ClientID==o.ClientID)).ToList(); var NewlyAdded=ListofNewClient.Where(n=>!ListofOldClient.Any(o=>o.ClientID==n.ClientID)).ToList();,c#,linq,reflection,C#,Linq,Reflection,以上功能适用于普通、已删除、新添加,但与旧客户端相比,我无法找到更新或修改的新客户端 List<NeWClient> ListofNewClient = new List < NeWClient >{ new NeWClient { ClientID = 1, ClientName = "Nilesh", companyLocation = "Mumbai", companyName = "CG" },

以上功能适用于普通、已删除、新添加,但与旧客户端相比,我无法找到更新或修改的新客户端

List<NeWClient> ListofNewClient = new List < NeWClient >{
                new NeWClient {   ClientID = 1, ClientName = "Nilesh", companyLocation = "Mumbai", companyName = "CG" },
                 new NeWClient {   ClientID = 101, ClientName = "Rahul", companyLocation = "USA", companyName = "TATA" }
               };

        List<OldClient> ListofOldClient = new List<OldClient>{
            new OldClient {   ClientID = 1, ClientName = "Nilesh", companyLocation = "Mumbai", companyName = "CG" },
            new OldClient {   ClientID = 3, ClientName = "Mary", companyLocation = "UK", companyName = "KG" },
            new OldClient {   ClientID = 7, ClientName = "jhon", companyLocation = "USA", companyName = "TG" },
            new OldClient {   ClientID = 9, ClientName = "Tom", companyLocation = "India", companyName = "PG" }
           };

    var  Common = ListofNewClient.Where(n => ListofOldClient.Any(o => o.ClientID == n.ClientID)).ToList();
    var  Deleted = ListofOldClient.Where(o => !ListofNewClient.Any(n => n.ClientID == o.ClientID)).ToList();
    var  NewlyAdded = ListofNewClient.Where(n => !ListofOldClient.Any(o => o.ClientID == n.ClientID)).ToList();
List listofNewClient t1=新列表{
新的新客户端{ClientID=200,ClientName=“Dinesh”,companyLocation=“Surat”,companyName=“nts”},
};
List ListofOldClient1=新列表{
新的旧客户端{ClientID=200,ClientName=“Dinesh”,companyLocation=“孟买”,companyName=“nts”},
新的旧客户端{ClientID=3,ClientName=“Mary”,companyLocation=“UK”,companyName=“KG”},
};
所需结果: 当我们与ListofOldClient1进行比较时,ListofNewClient1的值在这里发生了变化,因此所需的结果将来自下面给出的ListofNewClient1


ClientID=200,ClientName=“Dinesh”,companyLocation=“Surat”,companyName=“nts”

您可以将设置操作与自定义数据比较器一起使用

考虑到这些比较器:

        List<NeWClient> ListofNewClient1 = new List<NeWClient>{
            new NeWClient {   ClientID = 200, ClientName = "Dinesh", companyLocation = "Surat", companyName = "nts" },
           };

        List<OldClient> ListofOldClient1 = new List<OldClient>{
            new OldClient {   ClientID = 200, ClientName = "Dinesh", companyLocation = "Mumbai", companyName = "nts" },
            new OldClient {   ClientID = 3, ClientName = "Mary", companyLocation = "UK", companyName = "KG" },
           };
他们在你的“共同”群体中。您必须比较每个值以查看是否有任何更改。
class ClientComparer : IEqualityComparer<NeWClient>
{
    public bool Equals(NeWClient x, NeWClient y)
    {
        return x.ClientID == y.ClientID &&
        x.ClientName == y.ClientName &&
        x.companyLocation == y.companyLocation &&
        x.companyName == y.companyName;
    }

    public int GetHashCode(NeWClient obj)
    {
        unchecked
        {
            if (obj == null)
                return 0;
            int hashCode = obj.ClientID.GetHashCode();
            hashCode = hashCode * 23 + obj.ClientName.GetHashCode();
            hashCode = hashCode * 23 + obj.companyLocation.GetHashCode();
            hashCode = hashCode * 23 + obj.companyName.GetHashCode();
            return hashCode;
        }
    }
}

class NewClientIDComparer : IEqualityComparer<NeWClient>
{
    public bool Equals(NeWClient x, NeWClient y)
    {
        return x.ClientID == y.ClientID;
    }

    public int GetHashCode(NeWClient obj)
    {
        unchecked
        {
            if (obj == null)
                return 0;
            int hashCode = obj.ClientID.GetHashCode();
            return hashCode;
        }
    }
}
//common list
var common = ListofNewClient.Intersect(ListofOldClient, new NewClientIDComparer()).Select(lnc => lnc.ClientID);

var commonOriginal = ListofNewClient.Where(f => common.Contains(f.ClientID));
var commonOld = ListofOldClient.Where(f => common.Contains(f.ClientID));

var updates = commonOld.Except(commonOriginal, new ClientComparer());