asp.net 如何使用linq从JArray Groupby中提取行

rsl1atfo  于 2023-03-24  发布在  .NET
关注(0)|答案(2)|浏览(227)

我已经在JArray的列上应用了GroupBy,并在所有分组列表中循环,并从每个组中尝试获取“sectionGroup”列值为null的行。但当我尝试使用Linq时,它返回布尔列表。我以前从未使用过JArray。

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);
            }
        }

接下来我可以尝试什么?

cunj1qz1

cunj1qz11#

为了获取sectionGroup列值为空的行,应该使用Where方法而不是Select,然后才过滤元素。

var itemSectionGroupNull = item.Where(x => x["sectionGroup"] == null).ToList();
o8x7eapl

o8x7eapl2#

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
    }

相关问题