c# List<Tuple<string, string, int>> 按最小最大整数分组
c# List<Tuple<string, string, int>> group by min max int
我想按以下元组分组:
List<Tuple<string, string, int>> tu = new List<Tuple<string, string, int>>();
tu.Add(new Tuple<string, string, int>("a", "b", 201601));
tu.Add(new Tuple<string, string, int>("a", "b", 201602));
tu.Add(new Tuple<string, string, int>("a", "b", 201603));
tu.Add(new Tuple<string, string, int>("c", "d", 201601));
tu.Add(new Tuple<string, string, int>("c", "d", 201602));
新元组中的结果应如下所示:
//Item1, Item2, Min(Item2), Max(Item3)
List<Tuple<string, string, int, int>> newtu = new List<Tuple<string, string, int, int>>();
a,b,201601,201603
c,d,201601,201602
你能帮帮我吗?
from t in tu
group t by new { t.Item1, t.Item2 } into g
select Tuple.Create(g.Key.Item1, g.Key.Item2, g.Min(t => t.Item3), g.Max(t => t.Item3));
建议:不要在 C# 中使用元组。曾经
按匿名类型分组,然后在分组上使用 Min
+ Max
:
List<Tuple<string, string, int, int>> newtu = tu
.GroupBy(t => new { t1 = t.Item1, t2 = t.Item2 })
.Select(g => Tuple.Create(g.Key.t1, g.Key.t2, g.Min(t => t.Item3), g.Max(t => t.Item3)))
.ToList();
我想按以下元组分组:
List<Tuple<string, string, int>> tu = new List<Tuple<string, string, int>>();
tu.Add(new Tuple<string, string, int>("a", "b", 201601));
tu.Add(new Tuple<string, string, int>("a", "b", 201602));
tu.Add(new Tuple<string, string, int>("a", "b", 201603));
tu.Add(new Tuple<string, string, int>("c", "d", 201601));
tu.Add(new Tuple<string, string, int>("c", "d", 201602));
新元组中的结果应如下所示:
//Item1, Item2, Min(Item2), Max(Item3)
List<Tuple<string, string, int, int>> newtu = new List<Tuple<string, string, int, int>>();
a,b,201601,201603
c,d,201601,201602
你能帮帮我吗?
from t in tu
group t by new { t.Item1, t.Item2 } into g
select Tuple.Create(g.Key.Item1, g.Key.Item2, g.Min(t => t.Item3), g.Max(t => t.Item3));
建议:不要在 C# 中使用元组。曾经
按匿名类型分组,然后在分组上使用 Min
+ Max
:
List<Tuple<string, string, int, int>> newtu = tu
.GroupBy(t => new { t1 = t.Item1, t2 = t.Item2 })
.Select(g => Tuple.Create(g.Key.t1, g.Key.t2, g.Min(t => t.Item3), g.Max(t => t.Item3)))
.ToList();