我有一个列表,如果LineId是相同的,我将它们合并在一起.这一逻辑在所有情况下都适用,只有一种情况除外.在这里,这LineId可以是""null,如果是这样,我不想合并它.

LineId""null时,我怎么能不合并查询中的列表?

代码:

var groupedLines = lines
    .GroupBy(line => line.LineId)
    .Select(linesGroup => linesGroup.Aggregate((firstLine, nextLine) =>
    {
        // Logic is working as expected

        return firstLine;
    })).ToList();

我试着追随,但它仍然融合在一起

.GroupBy(line => !string.IsNullOrEmpty(line.LineId) )

示例1:(按预期工作)

输入

{
  "Lines": [
    {
      "Id": "1",
      "AccountingTotal": 10.2,
      "Description": "Description 1",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": "101" <-- Merge because it is same
    },
    {
      "Id": "2",
      "AccountingTotal": 10,
      "Description": "Description 2",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": "101" <-- Merge because it is same
    }
  ]
}

输出

{
  "Lines": [
    {
      "Id": "1|2",
      "AccountingTotal": 20.2,
      "Description": "Description 1|Description 1",
      "Price": "20.00",
      "Total": "20.00",
      "LineId": "101"
    }
  ]
}

示例2:(未按预期工作)

输入

{
  "Lines": [
    {
      "Id": "1",
      "AccountingTotal": 10.2,
      "Description": "Description 1",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": "" <-- Do not merge if this is "" or null
    },
    {
      "Id": "2",
      "AccountingTotal": 10,
      "Description": "Description 2",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": "" <-- Do not merge if this is "" or null
    }
  ]
}

预期结果

{
  "Lines": [
    {
      "Id": "1",
      "AccountingTotal": 10.2,
      "Description": "Description 1",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": ""
    },
    {
      "Id": "2",
      "AccountingTotal": 10,
      "Description": "Description 2",
      "Price": "10.00",
      "Total": "10.00",
      "LineId": "" 
    }
  ]
}

实际结果

{
  "Lines": [
    {
      "Id": "1|2",
      "AccountingTotal": 20.4,
      "Description": "Description 1|Description 2",
      "Price": "20.00",
      "Total": "20.00",
      "LineId": ""
    }
  ]
}

推荐答案

只需过滤集合before分组,使用Where方法排除空或空的LineId‘S:

var groupedLines = lines
     /* here --> */.Where(line => !string.IsNullOrEmpty(line.LineId))
                   .GroupBy(line => line.LineId)
                   .Select(linesGroup => linesGroup.Aggregate((firstLine, nextLine) =>
                   {
                       // Logic is working as expected
                       return firstLine;
                   }))
                   .ToList();

Csharp相关问答推荐

我应该将新的httpReportMessage()包装在using声明中吗?

更改对象的旋转方向

ASP.NET Core:如何在IPageFilter中注入ApplicationDbContext

哪个nuget包含SecurityStampValidatorOptions

在发布表单时绑定包含附加(嵌套)列表的对象列表的正确语法是什么

Select Many和默认IfEmpty内部Select Many错误工作

REST API端点中异步后台代码执行的类型

如何将不同类型的扩展参数的javascript函数转换成C#风格?

如何测量在使用UTF8而不是C#中的UTF16编码字符串时内存使用量的增长

是否由DI容器自动处理由ActivatorUilties.CreateInstance()创建的服务?

在平行内使用跨度.用于循环

Azure函数正在返回值列表,但该列表在Chrome中显示为空

Visual Studio如何使用当前的框架?

为什么我的自定义Json.net转换器不工作?

将两个for循环更改为一条LINQ语句

使用本地公共PEM文件加密字符串,使用Azure KayVault中的私钥解密

无法对包含字符串的列进行排序.请与实体框架联接

这是T自身的布尔表达式是什么意思?

CsvHelper在第二次迭代时抛出System.ObjectDisposedException

方法加载时出现类型加载异常