简体   繁体   English

有没有更优雅的方法来汇总多个属性?

[英]Is there a more elegant way to sum multiple properties?

I have a class which contains multiple properties of type Int32: 我有一个包含多个Int32类型的属性的类:

public class MyClass
{
    public int C1 { get; set; }
    public int C2 { get; set; }
    public int C3 { get; set; }
    .
    .
    .
    public int Cn { get; set; }
}

I want to sum all this properties. 我想总结所有这些属性。 Instead of doing: 而不是做:

int sum = C1 + C2 + C3 + ... + Cn

is there a more efficient/elegant method? 有没有更有效/更优雅的方法?

You can fake it, but I'm not sure how useful it is: 您可以伪造它,但是我不确定它有多有用:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new MyClass();
            // ...
            int sum = test.All().Sum();
        }
    }

    public class MyClass
    {
        public int C1 { get; set; }
        public int C2 { get; set; }
        public int C3 { get; set; }
        // ...
        public int Cn { get; set; }

        public IEnumerable<int> All()
        {
            yield return C1; 
            yield return C2; 
            yield return C3; 
            // ...
            yield return Cn; 
        }
    }
}                                                                                            

If you really want to perform the sum without having to type each property you can use reflection to iterate through your properties but this is involves a big performance cost. 如果您确实要执行求和而不需要键入每个属性,则可以使用反射来遍历属性,但这会带来很大的性能成本。 However, for fun you can do something like this: 但是,为了娱乐,您可以执行以下操作:

var item = new MyClass();
// Populate the values somehow
var result = item.GetType().GetProperties()
    .Where(pi => pi.PropertyType == typeof(Int32))
    .Select(pi => Convert.ToInt32(pi.GetValue(item, null)))
    .Sum();

PS: Don't forget to add using System.Reflection; PS:不要忘记using System.Reflection;添加using System.Reflection; directive. 指示。

Maybe you can use an array or a data structure which has the IEnumarable interfaces vs a custom class. 也许您可以使用具有IEnumarable接口与自定义类的数组或数据结构。 Then you can use linq to do Sum(). 然后,您可以使用linq执行Sum()。

If there's a strong enough need to store the values in separate members (properties, fields), then yes, that's the only way. 如果非常需要将值存储在单独的成员(属性,字段)中,那么是的,这是唯一的方法。 If you have a list of numbers however, store them in a list, not in separate members. 但是,如果有数字列表,请将其存储在列表中,而不要存储在单独的成员中。

Or, ugly: 或者,丑陋的:

new[]{C1,C2,C3,C4}.Sum()

But more characters than the single "+" anyway. 但是无论如何,字符比单个“ +”更多。

public class MyClass
{
    readonly int[] _cs = new int[n];

    public int[] Cs { get { return _cs; } }

    public int C1 { get { return Cs[0]; } set { Cs[0] = value; } }
    public int C2 { get { return Cs[1]; } set { Cs[1] = value; } }
    public int C3 { get { return Cs[2]; } set { Cs[2] = value; } }
    .
    .
    .
    public int Cn { get { return Cs[n-1]; } set { Cs[n-1] = value; } }
}

Now you can use Enumerable.Sum with MyClass.Cs , and you can still map C1 , C2 , ... to database fields. 现在,您可以将Enumerable.SumMyClass.Cs使用,并且仍可以将C1C2 ,...映射到数据库字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM