• <small id='WAKq9'></small><noframes id='WAKq9'>

  • <tfoot id='WAKq9'></tfoot>

    1. <legend id='WAKq9'><style id='WAKq9'><dir id='WAKq9'><q id='WAKq9'></q></dir></style></legend>
      • <bdo id='WAKq9'></bdo><ul id='WAKq9'></ul>
      <i id='WAKq9'><tr id='WAKq9'><dt id='WAKq9'><q id='WAKq9'><span id='WAKq9'><b id='WAKq9'><form id='WAKq9'><ins id='WAKq9'></ins><ul id='WAKq9'></ul><sub id='WAKq9'></sub></form><legend id='WAKq9'></legend><bdo id='WAKq9'><pre id='WAKq9'><center id='WAKq9'></center></pre></bdo></b><th id='WAKq9'></th></span></q></dt></tr></i><div id='WAKq9'><tfoot id='WAKq9'></tfoot><dl id='WAKq9'><fieldset id='WAKq9'></fieldset></dl></div>

      1. 对象上的Linq SUM?

        时间:2023-06-05
          <tbody id='lRotH'></tbody>
            <bdo id='lRotH'></bdo><ul id='lRotH'></ul>
          • <i id='lRotH'><tr id='lRotH'><dt id='lRotH'><q id='lRotH'><span id='lRotH'><b id='lRotH'><form id='lRotH'><ins id='lRotH'></ins><ul id='lRotH'></ul><sub id='lRotH'></sub></form><legend id='lRotH'></legend><bdo id='lRotH'><pre id='lRotH'><center id='lRotH'></center></pre></bdo></b><th id='lRotH'></th></span></q></dt></tr></i><div id='lRotH'><tfoot id='lRotH'></tfoot><dl id='lRotH'><fieldset id='lRotH'></fieldset></dl></div>
            <tfoot id='lRotH'></tfoot>

              • <small id='lRotH'></small><noframes id='lRotH'>

              • <legend id='lRotH'><style id='lRotH'><dir id='lRotH'><q id='lRotH'></q></dir></style></legend>

                  本文介绍了对象上的Linq SUM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个代表业务数据的对象.

                  I've an object that represent a business data.

                  基本上,它是一个汇总一些总数的对象:

                  Basically, it's an object agregating some totals:

                  public class MyClass{
                     public double MyDataOne {get;set;}
                     public double MyDataTwo {get;set;}
                     public double MyDataThree {get;set;}
                  
                     public static MyClass operator +(MyClass data1, MyClass data2){
                        return new MyClass{MyDataOne = data1.MyDataOne + data2.MyDataOne, MyDataTwo=data1.MyDataTwo+data2.MyDataTwo, MyDataThree=data1.MyDataThree+data2.MyDataThree };
                     }
                  }
                  

                  现在,如果我有一个 IEnumerable<MyClass>myClasses,有什么我可以在 MyClass 中实现的:?

                  Now, if I've an IEnumerable<MyClass> myClasses, Is there somethings I can implement in MyClass to make this:?

                  myClasses.Sum(d=>d);
                  

                  因为对我来说,添加对象的方式必须是对象而不是调用者的知识(如果有一天我多了一个数据,我不想查看我的整个代码来查看它的位置使用).

                  Because for me, the way an object is additioned must be the knowledge of the object and not the caller(if one day I've one data more, I don't want to look in my whole code to see where it is used).

                  谢谢

                  推荐答案

                  编写自己的扩展方法:

                  public static MyClass Sum(this IEnumerable<MyClass> source)
                  {
                      var result = new MyClass(); // all data will be zeros
                  
                      foreach(var item in source)
                         result = result + item;
                  
                      return result;
                  }
                  

                  用法:

                  var sum = myClasses.Sum();
                  

                  这篇关于对象上的Linq SUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Sum 多维数组 C# 下一篇:带有 CASE 语句和 SUM 函数的 LINQ 查询

                  相关文章

                  最新文章

                    <legend id='rpRht'><style id='rpRht'><dir id='rpRht'><q id='rpRht'></q></dir></style></legend>
                    <tfoot id='rpRht'></tfoot>

                      <bdo id='rpRht'></bdo><ul id='rpRht'></ul>

                    1. <i id='rpRht'><tr id='rpRht'><dt id='rpRht'><q id='rpRht'><span id='rpRht'><b id='rpRht'><form id='rpRht'><ins id='rpRht'></ins><ul id='rpRht'></ul><sub id='rpRht'></sub></form><legend id='rpRht'></legend><bdo id='rpRht'><pre id='rpRht'><center id='rpRht'></center></pre></bdo></b><th id='rpRht'></th></span></q></dt></tr></i><div id='rpRht'><tfoot id='rpRht'></tfoot><dl id='rpRht'><fieldset id='rpRht'></fieldset></dl></div>
                    2. <small id='rpRht'></small><noframes id='rpRht'>