我有一个包含以下数据的列表:
From To Gender departure date
Delhi Mumbai M 16-Feb-18
Delhi Mumbai M 16-Feb-18
Delhi Mumbai F 16-Feb-18
Delhi Mumbai C 16-Feb-18
Mumbai Mohili F 16-Feb-18
Mumbai Mohili M 16-Feb-18
Mumbai Mohili C 16-Feb-18
我想根据From
和To
列对此列表进行分组,并通过LINQ计算每组中有多少男性,女性和儿童。我怎样才能做到这一点。
我把它分组,但现在如何计算男性和女性的数量。
var list = result.Journey.GroupBy(x => new { x.From, x.To });
答案 0 :(得分:1)
尝试以下代码,
var list = travelars.GroupBy(x => new { x.From, x.To }).ToList().Select(e => new {
MaleCount = e.Count(g => g.Gender == "M"),
FemaleCount = e.Count(g => g.Gender == "F"),
ChildCount = e.Count(g => g.Gender == "C"),
});
答案 1 :(得分:1)
要知道的重要事项是GroupBy
的返回类型是IGrouping<TKey, TElement>
。此分组是可枚举的,并在枚举时生成元素。因此,分组可能是任何其他可枚举的LINQ查询。
var query = result.Journey
.GroupBy(x => new { x.From, x.To })
.Select(g => new
{
From = g.Key.From,
To = g.Key.To,
MCount = g.Count(x => x.Gender == "M"),
FCount = g.Count(x => x.Gender == "F"),
CCount = g.Count(x => x.Gender == "C")
});
答案 2 :(得分:0)
您似乎不明白GroupBy
返回的内容。它返回一堆分组。每个IGrouping<T, U>
本身就是该组中IEnumerable<U>
个事物。
我建议你做的是按性别分组再次分组!这样,即使已将新的性别添加到模型中,您也不必添加代码来计算新的性别。
// using the new C# 7 tuple syntax!
foreach(var group in Journeys.GroupBy(x => (x.To, x.From))) {
Console.WriteLine($"People traveling from {group.Key.Item1} to {group.Key.Item2}:");
foreach (var subgroup in group.GroupBy(x => x.Gender)) {
Console.WriteLine($"{subgroup.Key}: {subgroup.Count()} people");
}
}
答案 3 :(得分:-2)
你可以试试这个,
List<Journey> list = new List<Journey>();
list.Add(new Journey() {From="Delhi",To="Mumbai",Gender=Gender.MALE });
list.Add(new Journey() { From = "Delhi", To = "Mumbai", Gender = Gender.MALE });
list.Add(new Journey() { From = "Delhi", To = "Mumbai", Gender = Gender.FEMALE });
list.Add(new Journey() { From = "Delhi", To = "Mumbai", Gender = Gender.CHILD });
list.Add(new Journey() { From = "Delhi", To = "Mumbai", Gender = Gender.MALE });
list.Add(new Journey() { From = "Delhi", To = "Mumbai", Gender = Gender.MALE });
list.Add(new Journey() { From = "Mumbai", To = "Mohili", Gender = Gender.FEMALE });
list.Add(new Journey() { From = "Mumbai", To = "Mohili", Gender = Gender.CHILD });
list.Add(new Journey() { From = "Mumbai", To = "Mohili", Gender = Gender.CHILD });
var grp = list.GroupBy(s => new { s.From, s.To }).ToDictionary(d => d.Key, d => d.Select(i=>new JourneyCalc { From=i.From,To=i.To,Gender=i.Gender}).ToList());
foreach (var item in grp)
{
var str = from val in item.Value group val by val.Gender into itmgrp select new JourneyCalc { Gender = itmgrp.Key, JourneyCount = itmgrp.Count() };
foreach (JourneyCalc jc in str)
{
Console.WriteLine(value: $"{item.Value[0].From} - {item.Value[0].To} => {jc.Gender} - {jc.JourneyCount}");
}
}
public class JourneyCalc
{
public string From { get; set; }
public string To { get; set; }
public Gender Gender { get; set; }
public int JourneyCount { get; set; }
}
public enum Gender
{
MALE,
FEMALE,
CHILD
}
public class Journey
{
public string From { get; set; }
public string To { get; set; }
public Gender Gender { get; set; }
}
Output:
Delhi - Mumbai => MALE - 4
Delhi - Mumbai => FEMALE - 1
Delhi - Mumbai => CHILD - 1
Mumbai - Mohili => FEMALE - 1
Mumbai - Mohili => CHILD - 2