如何使用Linq从Json或类对象中查找子元素

时间:2019-06-12 11:06:56

标签: c# linq

我有一个保存JSON记录的对象,我想从JSON获取内部子对象。

下面是示例JSON,dbSettings拥有此JSON的所有属性。

{
  "Settings": [
  {
              "Category": "Bodies 0",
              "MDName": "CompleteDispensers",
              "subSettings": [
                {
                  "Category": "CompleteDispensers",
                  "MDName": "CompleteDispensers 0",
                  "subSettings": [
                    {
                      "Category": "CompleteDispensers 0",
                      "MDName": "dispenserTypeID",
                      "subSettings": null
                    },
                    {
                      "Category": "CompleteDispensers 0",
                      "MDName": "productID",
                      "subSettings": null
                    }
                  ]
                },
                {
                  "Category": "CompleteDispensers",
                  "MDName": "CompleteDispensers 1",
                  "subSettings": [
                    {
                      "Category": "CompleteDispensers 1",
                      "MDName": "dispenserTypeID",
                      "subSettings": null
                    },
                    {
                      "Category": "CompleteDispensers 1",
                      "MDName": "productID",
                      "subSettings": null
                    }
                  ]
                }
              ]
            }
  ]
}

我尝试了下面的哪个代码来获取空记录。

var obj = dbSettings.Settings.FirstOrDefault(_ => _.Name == 
            Constants.DispenseIDName);

当我尝试如下操作时,我将获得预期的价值。

var result = itemTitle > itemTitle1 ? itemTitle : itemTitle1;

if (dbSettings.Settings[9].subSettings[0].subSettings[14].Name == 
        Constants.DispenseIDName)
            {
                dbSettings.Settings[9].subSettings[0].subSettings[14].Value = 
                 result.ToString();
            }

我希望这段代码是动态的。而不是使用索引值

Settings [9] .subSettings [0] .subSettings [14],我希望使用Linq或其他任何方法在C#中使用类对象查询此对象。

按照建议,我尝试获取空引用异常。

 if (dbSettings.Settings.SelectMany(x => x.subSettings)!=null)
            {
                if (dbSettings.Settings.SelectMany(x => x.subSettings).SelectMany(x => x.subSettings) != null)
                {
                    if (dbSettings.Settings.SelectMany(x => x.subSettings).SelectMany(x => x.subSettings).Any(x => x.Name == Constants.DispenseIDName))
                    {
                        dbSettings.Settings.SelectMany(x => x.subSettings).SelectMany(x => x.subSettings).Any(x => x.Value == result.ToString() );
                    }
                }
            }  

在这里,对于所有内部记录,子集中都没有数据。因此,我保留了空检查条件并进行了尝试,但是它抛出了空引用异常。

任何建议!

1 个答案:

答案 0 :(得分:0)

dbSettings.SelectMany(x=>xSettings).SelectMany(x=>x.subSettings).SelectMany(x=>x.subSettings).Any(x=>x.Name == Constants.DispenseIDName);

如果任何名称等于所需名称,它将返回true。

嵌套int列表示例:

List<List<List<List<int>>>> myMegaList = new List<List<List<List<int>>>>(){
new List<List<List<int>>>{
new List<List<int>>{
new List<int>{
2,4,5,6,7
}
}
}

};

myMegaList.SelectMany(x=>x).SelectMany(x=>x).SelectMany(x=>x).Any(x=>x==2);