LINQ groupby、划分和提取
LINQ groupby, divide and extract
我的程序正在将获取的相同数据存储在元组列表中
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
});
您的 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
});
});
我的程序正在将获取的相同数据存储在元组列表中
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
});
您的 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
});
});