简体   繁体   English

如何使用Linq从两个字典列表中获取唯一值?

[英]How to get unique values from two list of dictionaries using Linq?

This is my code 这是我的代码

public class Model
{
    public Model();

    public Dictionary<string, string> Data { get; set; }

}

list<dictionary<string,string>>  data1;
list<dictionary<string,string>>  data2;

data1=await get<model>();
data2=await get<model>();

data1[0]=[0][{id,101}]
         [1][{name,one}]
         [2][{marks,56}]
         [3][{state,ap}]     

data1[1]=[0][{id,102}]
         [1][{name,two}]
         [2][{marks,65}]
         [3][{state,up}]     

data1[2]=[0][{id,103}]
         [1][{name,three}]
         [2][{marks,89}]
         [3][{state,usa}]     



data2[0]=[0][{roleid,101}]
         [1][{stdname,one}]
data2[1]=[0][{roleid,102}]
         [1][{stdname,two}]

Finally i want Output Like 最后我想输出像

data3[0]=[0][{id,103}]
         [1][{name,three}]
         [2][{marks,89}]
         [3][{state,usa}]     

in the above code i have two list of dictionaries, i want to get unqiue id values compare two lists.the above two lists key names are different get except values from two lists based on first list id and second list roleid. 在上面的代码中我有两个字典列表,我希望得到unqiue id值比较两个列表。上面两个列表的键名称是不同的,除了基于第一个列表id和第二个列表roleid的两个列表中的值。

Assuming you would like to intersect to lists: 假设你想要与列表相交:

Use the intersect method. 使用intersect方法。 First implement a comparer between two dictionaries: 首先在两个字典之间实现一个比较器:

public class DictComparer : IEqualityComparer<Dictionary<string, string>>
{
    public bool Equals(Dictionary<string, string> x, Dictionary<string, string> y)
    {
        return (x == y) || (x.Count == y.Count && !x.Except(y).Any());
    }

    public int GetHashCode(Dictionary<string, string> x)
    {
        var ret = 123;
        foreach (var keyValue in x)
        {
            ret = ret + (keyValue.GetHashCode() * 31);
        }
        return ret;
    }
}

Then use it like this: 然后像这样使用它:

var result = data1.Union(data2).Except(data1.Intersect(data2, new DictComparer()), new DictComparer());

Edit: noticed he wanted the exact opposite. 编辑:注意到他想要完全相反。 Changed the function accordingly. 相应地改变了功能。 Edit2: I was missing a proper implementation of GetHashCode for dictionaries. Edit2:我错过了正确的字典GetHashCode实现。 This implementation seems to work: link 这个实现似乎有效: 链接

If you want to compare the dictionaries using their Id you must implement a custom comparer 如果要使用其ID比较字典,则必须实现自定义比较器

public class DictComparerById : IEqualityComparer<Dictionary<string, string>>
{
    public bool Equals(Dictionary<string, string> x, Dictionary<string, string> y)
    {
        // Two dictionary are equal if the have the same "id"
        string idX;
        if(!x.TryGetValue("id", out idX))
            x.TryGetValue("roleid", out idX);

        string idY;
        if(!y.TryGetValue("id", out idY))
            y.TryGetValue("roleid", out idY);

        return (idX == idY);
    }

    public int GetHashCode(Dictionary<string, string> x)
    {
        string id;
        if(!x.TryGetValue("id", out id))
            x.TryGetValue("roleid", out id);
        return id.GetHashCode();
    }
}

Then you can use 然后你可以使用

var dictCmp = new DictComparerById();
var data3 = data1
    .Union(data2, dictCmp)
    .Except(data1.Intersect(data2, dictCmp), dictCmp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM