使用LINQ过滤掉C#中两个List <jobject>的设置差异

时间:2018-05-14 14:27:09

标签: c# arrays json linq json.net

我有2 List<JObject>要比较他们的设定差异,

要比较的两个List<JObject>的格式如下:

这是我的基地List<JObject>的JSON结构:

[
    {
        "name": "competency",
        "subLists": [
            {
                "canonicalForm": "c1",
                "list": [
                    "c one",
                    "c 1",
                    "cone",
                    "Cone"
                ]
            }
        ],
        "roles": []
    },
    {
        "name": "demoList",
        "subLists": [],
        "roles": []
    }
]

这是List<JObject>的JSON结构,我希望找到与之相比的设置差异:

[
    {
        "name": "competency",
        "subLists": [
            {
                "canonicalForm": "c1",
                "list": [
                    "c one",
                    "c 1",
                    "cone",
                    "Cone",
                    "C ONE"
                ]
            },
            {
                "canonicalForm": "c2",
                "list": [
                    "c two",
                    "c 2"
                ]
            }
        ],
        "roles": []
    },
    {
        "name": "leavetype",
        "subLists": [
            {
                "canonicalForm": "annual",
                "list": [
                    "Easter"
                ]
            }
        ],
        "roles": []
    },
    {
        "name": "demoList",
        "subLists": [],
        "roles": []
    }
]

输出设置差异JSON为:

[
    {
        "name": "competency",
        "subLists": [
            {
                "canonicalForm": "c1",
                "list": [
                    "c one",
                    "c 1",
                    "cone",
                    "Cone",
                    "C ONE"
                ]
            },
            {
                "canonicalForm": "c2",
                "list": [
                    "c two",
                    "c 2"
                ]
            }
        ],
        "roles": []
    },
    {
        "name": "leavetype",
        "subLists": [
            {
                "canonicalForm": "annual",
                "list": [
                    "Easter"
                ]
            }
        ],
        "roles": []
    }
]

我尝试使用以下代码使用两个for循环:

List<JObject> baseList = JsonConvert.DeserializeObject<List<JObject>>(baseListString);
List<JObject> comparedList = JsonConvert.DeserializeObject<List<JObject>>(comparedListString);

List<JObject> finalResultList = new List<JObject>();


for (int i = 0; i < baseList.Count; i++)
{
     for (int j = 0; i < comparedList.Count; j++)
     {
        if(baseList[i]["subLists"] != comparedList[j]["subLists"])
                        finalResultList.Add(comparedList[j]);
     }
}

但这不是削减它,我想根据JObjectnamecanonicalForm键的关键值来比较整个list,并过滤掉该集合使用LINQ的差异。但我无法这样做。

我尝试使用.Except()扩展方法,但效果不佳。

修改:

我想检查3个密钥是否有任何变化:namecanonicalFormlist,如果compareObject与BaseObject I相比有任何变化想要返回有更改的JObject。输出显示在上面。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我终于通过为IEqualityComparer实施JObject来解决了我的问题。

这是我的JObjectEqualityComparer课程:

public class JObjectEqualityComparer : IEqualityComparer<JObject>
    {
        public bool Equals(JObject x, JObject y)
        {
            if (x == null && y == null)
                return true;
            if ((x != null && y == null) || (x == null && y != null))
                return false;

            return JObject.DeepEquals(x, y);
        }

        public int GetHashCode(JObject obj)
        {
            JTokenEqualityComparer comparer = new JTokenEqualityComparer();
            int hashCode = comparer.GetHashCode(obj);
            return hashCode;
        }
    }

最后我使用了.Except()扩展方法的重载来查找设置差异。

enter image description here

以下是代码:

List<JObject> baseList = JsonConvert.DeserializeObject<List<JObject>>(baseListString);
List<JObject> comparedList = JsonConvert.DeserializeObject<List<JObject>>(comparedListString);

List<JObject> finalResultList = new List<JObject>();

finalResultList = comparedList.Except(baseList, new JObjectEqualityComparer()).ToList();

感谢Ed PlunkettdbcCoding Yoshi将我指向正确的方向。