用于查询的 Lambda 表达式根据条件选择不同的值

发布于 2024-10-02 15:35:44 字数 592 浏览 1 评论 0原文

我正在 C# 中使用 lambda 表达式。我试图根据类型选择一个值,然后对结果求和。

var  length = (from al in tmp
                from n in al
                 where n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2"
                 select n.trippath.length).sum();

这工作正常,但我的问题是我需要在求和之前将其他类型的值设置为默认值 0,类似于

   List<double> ActualLength = new List<double>();

   if( n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2")
       ActualLength.Add(n.trippath.length)
   else
       ActualLength.Add(0);

   return ActualLength.sum();

I am working with lambda expression in c#. I am trying to select a value based on type and then the resultant is summed.

var  length = (from al in tmp
                from n in al
                 where n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2"
                 select n.trippath.length).sum();

this works fine but my issue is i need to set vales for other types to a defalute value 0 before it is summed, similar to

   List<double> ActualLength = new List<double>();

   if( n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2")
       ActualLength.Add(n.trippath.length)
   else
       ActualLength.Add(0);

   return ActualLength.sum();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名的好友 2024-10-09 15:35:44

如果我理解正确,你想要这样的东西:

var projection =
    tmp.SelectMany(x => x)
       .Select(x => {
           if(x.TripPath.DataType == "type1" || x.TripPath.DataType == "type2") {
               return new { Item = x, Length = x.TripPath.Length };
           }
           else {
               return new { Item = x, Length = 0 };
           }
       });

那么你可以说

int length = projection.Sum(x => x.Length);

这就是你如何完成你想要做的事情(将与谓词匹配的每个项目投影到其长度,否则将其投影为零)。

但是,您的使用情况看起来甚至没有使用投影。在这种情况下,您可以逃脱与

return tmp.SelectMany(x => x)
          .Where(x => 
              x.TripPath.DataType == "type1" ||
              x.TripPath.DataType == "type2"
          )
          .Sum(x => x.TripPath.Length);

谓词不匹配的项目不会命中求和的序列,因此它们不会对总和做出任何贡献。

If I understand correctly, you want something like this:

var projection =
    tmp.SelectMany(x => x)
       .Select(x => {
           if(x.TripPath.DataType == "type1" || x.TripPath.DataType == "type2") {
               return new { Item = x, Length = x.TripPath.Length };
           }
           else {
               return new { Item = x, Length = 0 };
           }
       });

Then you can say

int length = projection.Sum(x => x.Length);

That is how you can accomplish what it seems like you are trying to do (project each item matching a predicate to its length and otherwise project it to zero).

However, your usage looks like you aren't even using the projections. In which case, you can get away with

return tmp.SelectMany(x => x)
          .Where(x => 
              x.TripPath.DataType == "type1" ||
              x.TripPath.DataType == "type2"
          )
          .Sum(x => x.TripPath.Length);

Items that don't match the predicate don't hit the sequence that is summed over and therefore they don't contribute anything to the sum.

爱殇璃 2024-10-09 15:35:44

您可以像这样使用三元条件:

List<double> ActualLength = new List<double>();
var TotalActualLength =  ActualLength.sum(n=> n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2" ? n.trippath.length : 0);

   

You can use ternary condition like that:

List<double> ActualLength = new List<double>();
var TotalActualLength =  ActualLength.sum(n=> n.TripPath.DataType =="type1" || n.tripPath.DataType == "Type2" ? n.trippath.length : 0);

   
风吹雪碎 2024-10-09 15:35:44

看一下 OfType 扩展方法。然后,您可以投影到匿名类型以分配默认值并对其进行求和。

Take a look at the OfType extension method. You can then project into an anonymous type to assign a default value and .Sum off this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文