如何将此转换为使用Linq到数据集?

Cor*_*ian 2 c# linq

如何将此转换为使用Linq到数据集?

foreach (Row row in Rows) 
{ 
    if (row.material> 0) 
        sumtid += row.time * row.material; 
    else 
        sumtid += row.time; 
} 
Run Code Online (Sandbox Code Playgroud)

Mar*_*zek 7

var sumtid = Rows.Sum(x => x.time * (x.materials > 0 ? x.materials : 1));
Run Code Online (Sandbox Code Playgroud)

要么

var sumtid = Rows.Sum(x => x.materials > 0 ? x.time * row.materials : x.time);
Run Code Online (Sandbox Code Playgroud)

我假设你的Rows变量是IEnumerable<Row>.

编辑

解决问题的另一种方法:

var sumtid = Rows.Select(x => x.materials > 0 ? x.time * row.materials : x.time)
                 .Sum();
Run Code Online (Sandbox Code Playgroud)