C#通用列表比较并获取不匹配的行

时间:2019-03-22 21:33:19

标签: c# list compare

我在C#中有两个列表。我需要一个LINQ查询来仅比较和列出不匹配的行。

List<First> firstList;
List<Second> secondList;
both have the unique property called ID;

firstList:

ID  Desc1   Desc2           
1   aaa     mmm             
2   bbb     fff             
3   ccc     ttt   
4   ddd     yyy
5   eee     ggg

secondList:

 
ID  Desc1   Desc2   Status          
1   aaa     mm      P       
2   bbb     fff     S       
3   ccc     ttt     P   
4   ddd     yy      S
5   eee     ggg     P

结果:

 
ID  Desc1   Desc2   Status
2   aaa     mm      P
4   ddd     yy      S

我想将firstList中的Desc1和Desc2与secondList进行比较,并需要不匹配的项目状态。

2 个答案:

答案 0 :(得分:1)

一些找到结果的方法(lambda,linq):

var query0 = secondList.Where(item => firstList.All(f => !item.Desc1.Equals(f.Desc1)  || !item.Desc2.Equals(f.Desc2)));

var query1 =
    from item in secondList
    where firstList.All(f => !item.Desc1.Equals(f.Desc1) || !item.Desc2.Equals(f.Desc2))
    select item;

如果您希望结果在列表中而不是枚举,则只需在末尾添加Tolist():

var list0 = secondlist.Where(item => firstlist.All(f => !item.Desc1.Equals(f.Desc1) || !item.Desc2.Equals(f.Desc2))).ToList();

要在评论中回答您的问题,

如果要迭代或选择特定的ID,可以执行以下操作:

        foreach(var item in query0)
        {
           Console.WriteLine($"ID:{item.ID}, Desc1:{item.Desc1}, Desc2:{item.Desc2}, Status:{item.Status}");  
        }

        //if you want to query a particular id:
        var queryid = query0.Where(item => item.ID == particularID);

答案 1 :(得分:0)

请参阅下文。您需要在两个列表之间进行合并,然后删除交集。您将需要实现IEqualityComparer接口。

void Main()
{
    List<UserProfile> u1 = new List<UserQuery.UserProfile>(){
        new UserProfile(){ID = 1, FirstName  = "Super" , LastName = "Man"},
        new UserProfile(){ID=2, FirstName = "Spider", LastName="Man"},
        new UserProfile(){ID=3, FirstName = "Bat", LastName="Man"}
    };

    List<UserProfile> u2 = new List<UserProfile>(){
        new UserProfile(){ID = 1, FirstName="Super", LastName="Man"},
        new UserProfile(){ID = 2, FirstName="Thor", LastName="Ragnarok"},
        new UserProfile(){ID = 3, FirstName="Hulk", LastName="Baner"}
    };

//  u1.Intersect(u2, new UserProfileComparer())
//      .ToList()
//      .ForEach(x => Console.WriteLine(x.Name));

//  u1.Except(u2, new UserProfileComparer())
//      .ToList()
//      .ForEach(x => Console.WriteLine(x.Name));

    // (u1 + u2) - (u1*u2)
    u1.Union(u2, new UserProfileComparer())
        .Except(u1.Intersect(u2, new UserProfileComparer()))
        .Distinct()
        .ToList()
        .ForEach(x => Console.WriteLine(x.Name));
}

class UserProfile
{
    public int ID {get;set;}
    public string Name => LastName + "," + FirstName;
    public string FirstName {get;set;}
    public string LastName {get;set;}
}

class UserProfileComparer : IEqualityComparer<UserProfile>
{
    public bool Equals(UserProfile item1, UserProfile item2)
    {
        if (object.ReferenceEquals(item1, item2))
            return true;
        if (item1 == null || item2 == null)
            return false;
        return item1.FirstName.Equals(item2.FirstName) &&
               item1.LastName.Equals(item2.LastName);
    }

    public int GetHashCode(UserProfile item)
    {
        return new { item.FirstName, item.LastName }.GetHashCode();
    }
}