skip to Main Content

I have applied GroupBy on columns of JArray and looping through all grouped list and from each group trying to get rows where "sectionGroup" column value is null. But when I try with Linq, it’s returning Boolean list. I have never worked with JArray before.

public Task<JArray> JLogData(JArray jArray)
        {
            var groupData = jArray.GroupBy(g => new { modelName = g["modelName"], nlpModelVersion = g["nlpModelVersion"], docId = g["docId"] });
            foreach (var item in groupData)
            {
                // This is returning Boolean list. and I am trying go get list
                var itemSectionGroup = item.Select(x => x["sectionGroup"] == null);
                var itemSectionGroupNull = item.Select(x => x["sectionGroup"] != null);
            }
        }

What can I try next?

2

Answers


  1. In order to get the rows where the sectionGroup column value is null, you should use the Where method instead of Select, and only then filter the elements.

    var itemSectionGroupNull = item.Where(x => x["sectionGroup"] == null).ToList();
    
    Login or Signup to reply.
  2. JArray jsonArray = JArray.Parse("your json string here");
        
        // Group the items in the JArray by a specific property
        var groupedItems = jsonArray.GroupBy(x => (string)x["PropertyName"]);
        
        // Extract the rows from the grouped items
        var extractedRows = groupedItems.SelectMany(group =>
        {
            // Select the rows from the current group
            var rows = group.Select(x => new
            {
                Property1 = (string)x["Property1"],
                Property2 = (string)x["Property2"],
                // add other properties as needed
            });
            
            // Return the rows as an IEnumerable
            return rows;
        });
        
        // Use the extracted rows as needed
        foreach (var row in extractedRows)
        {
            // Do something with the row
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search