我已經爲您的要求實現了控制檯應用程序代碼。 這是工作代碼。
首先它通過ID進行分組,然後總結每個ID組中的得分ID 最終結果是ID &的字典對此ID的得分總和。
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var records = new[] {
new Records{ID=100,ListNum=1,Value=2,Score=10},
new Records{ID=300,ListNum=1,Value=2,Score=9},
new Records{ID=200,ListNum=1,Value=4,Score=11},
new Records{ID=100,ListNum=2,Value=3,Score=10.5},
new Records{ID=200,ListNum=2,Value=3,Score=10}
};
var idScoreSumDictionary = records
.GroupBy(rec => rec.ID)
.ToDictionary(
rec => rec.Key,
rec => rec.Sum(x => x.Score)
);
foreach (var item in idScoreSumDictionary)
{
Console.WriteLine("ID: {0}",item.Key);
Console.WriteLine("Its Score Sum: {0}",item.Value);
}
}
}
class Records
{
public int ID { get; set; }
public int ListNum { get; set; }
public int Value { get; set; }
public double Score { get; set; }
}
}
向我們展示您的代碼,以及您到目前爲止所嘗試的內容。與此說,我會建議使用'System.Linq',將使你的生活更容易。 – Banana 2014-09-20 13:56:43
這將有助於看到一些輸入示例以及每個輸入的預期輸出。 – 2014-09-20 14:13:12
我正在寫一個包含500多行的算法,並且我使用了不同的函數和類。所以,它需要你的時間來通過它們。我試圖以最好的方式解釋我想在這部分做什麼。我知道這不是很清楚,但我無法以更好的方式解釋。如果您能幫助我,我將不勝感激。你能解釋一下如何使用linq來達到這個目的嗎? – 2014-09-20 14:27:01