我的程式將相同的獲取資料存盤在元組串列中
public List<Tuple<int, double, double, double, double>> Temp = new List<Tuple<int, double, double, double, double>>();
以此為例
Temp.Add(new Tuple<int, double, double, double, double>(1, 1.1, 0.4, 0, 100.1));
Temp.Add(new Tuple<int, double, double, double, double>(2, 1.1, 0.4, 1.1, 99.1));
Temp.Add(new Tuple<int, double, double, double, double>(2, 1.1, 0.4, 1.3, 55.1));
Temp.Add(new Tuple<int, double, double, double, double>(1, 2.2, 0.4, 0, 100.2));
Temp.Add(new Tuple<int, double, double, double, double>(2, 2.2, 0.4, 1.1, 50.2));
我必須匯總資料,以便回傳具有相同 T2 和 T3 的元組及其 T4 和 T5 除以具有 T2 和 T3 的第一個元組的 T5。
如果我們看這個例子,前 3 個元組有相同的 T2 和 T3,所以我的最終結果是
[2, 1.1, 0.4, 1.1, (99.1/100.1)] //<- the 2nd tuple with its T5 divided by the 1st tuple T5
[2, 1.1, 0.4, 1.3, (55.1/100.1)] //<- the 3rdtuple with its T5 divided by the 1st tuple T5
那么第 4 和第 5 個元組也有相同的 T2 和 T3,所以:
[2, 2.2, 0.4, 1.1, (50.2/100.2)] //<- the 5th tuple with its T5 divided by the 4th tuple T5
我被困在:
var norm_result = Temp.GroupBy(x => new { x.Item2, x.Item3 })
.Select(x => new
{
AA = x.Key.Item2,
BB = x.Key.Item3,
CC = ??, //Item4
DD = ??, // Item5 divided by its first value in the group
});
uj5u.com熱心網友回復:
您的陳述句中的每個組GroupBy都有該組中的專案的集合,以及該組的專案Key,因此在計算您的規范結果時,您需要列舉每個組來進行計算。
var norm_result = Temp.GroupBy(x => new{ x.Item2, x.Item3 })
//Since this select statement returns IEnumerables for each grouping
//this will be an IEnumerable of IEnumerables. If you want to flatten
//things into a single IEnumerable you could do a SelectMany here.
.Select(x =>
{
//sort by Item1, hopefully the order of the items after the first
//doesn't matter since they all seem to be a value of 2.
var sorted = x.OrderBy(item => item.Item1).ToList();
//this is the first item in the group that has Item1 == 1
var first = sorted[0];
//skip the first since it appears that you are wanting to skip
//the first value in your norm results and the select here will
//return an IEnumerable of the remaining items in the group with
//the dividing logic
return sorted.Skip(1).Select(item => new
{
item.Item1,
item.Item2,
item.Item3,
item.Item4,
Item5 = item.Item5 / first.Item5
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/460848.html
下一篇:為呼叫者恢復C 流的例外掩碼
