Search for the ColumnName in the provided JSON using Linq

Viewed 27

Request for help on extracting the list of name from the provided JSON. for instance we have a list under datadetailslist from which i need to search for ColumnName = Column12 and then provide with me all the list of names containing the columname.

here we have a list under one more list, as i need to search for the inner list to and get me the names from the outlist.

this is what i have tried.

List<DataStructureModel> dataStructureList = desrialized to this object
var datadetails = dataStructureList.SelectMany(x => x.DataDetailsList);
var colName = datadetails.Where(x => x.ColumnName.Equals("COLUMN12")).ToList();

with the above two lines i was able to extract the total number records matching with "Column12" but was not able to get the Name which is available in the first / primary list.

var res = dataStructureList.SelectMany(x => x.DataDetailsList).Where(x => x.ColumnName.Equals("COLUMN12")).ToList();

as we have done the SelectMany in the firstplace i am loosing the control of the parent list.
[
  {
    "ID": 1,
    "Name": "AccountName",
    "DataDetailsList": [
      {
        "ColumnName": "Column1",
        "Type": "text",
        "Length": "10"
      },
      {
        "ColumnName": "Column12",
        "Type": "text",
        "Length": "255"
      }
    ]
  },
  {
    "ID": 2,
    "Name": "AccountName1",
    "DataDetailsList": [
      {
        "ColumnName": "Column11",
        "Type": "text",
        "Length": "10"
      },
      {
        "ColumnName": "Column12",
        "Type": "text",
        "Length": "255"
      }
    ]
  }
]
0 Answers
Related