私のプログラムでは、カスタムクラスのリストを作成しました。このクラスには整数プロパティが含まれています。他の指定された整数をリスト内のこれらのプロパティの合計で除算したいのですが、除算でSum()
正しく機能するようにシームされていません。
私はあなたのために短いデモを作成しました、それは単純なコンソールアプリケーションです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sum
{
class Program
{
static void Main(string[] args)
{
List<Class> list = new List<Class>() {
new Class() { Property = 1},
new Class() { Property = 1},
new Class() { Property = 1},
new Class() { Property = 1},
new Class() { Property = 1},
new Class() { Property = 1}
};
Console.WriteLine("Sum: " + list.Sum(x => x.Property));
Console.WriteLine("Division: " + (2 / list.Sum(x => x.Property)));
Console.ReadLine();
}
}
public class Class
{
public int Property { get; set; }
}
}
プロパティの合計を新しい変数に保存し、後でそれを除算に使用できることを知っています。しかしSum
、この状況でうまく機能するべきではありませんか?