我有C#数据,想要使用正常数据,如下所示
ID Count Client MessageType
1 100 MegaTech Missing SO
2 100 MegaTech Not shipped
3 100 NIXIMIXI No PDF
4 100 MegaTech Missing SO
5 100 NIXIMIXI Not shipped
6 100 MegaTech No PDF
7 100 NIXIMIXI Other
8 100 MegaTech Other
9 100 MegaTech No PDF
10 100 NIXIMIXI Missing SO
11 100 MegaTech Not shipped
12 100 NIXIMIXI No PDF
13 100 MegaTech Missing SO
14 100 NIXIMIXI Not shipped
15 100 NIXIMIXI No PDF
16 100 MegaTech other
17 100 NIXIMIXI other
18 100 NIXIMIXI other
所以我想要像下面这样放置列表,消息Type other可能包含空格或空值。
ID Count Client MessageType
1 100 MegaTech Missing SO
2 100 MegaTech Missing SO
3 100 MegaTech No PDF
4 100 MegaTech No PDF
5 100 MegaTech No PDF
6 100 MegaTech Not shipped
7 100 MegaTech Not shipped
8 100 MegaTech Other
9 100 MegaTech Other
10 Total For 9 Total Items 900
11 100 NIXIMIXI Missing SO
12 100 NIXIMIXI Missing SO
13 100 NIXIMIXI No PDF
14 100 NIXIMIXI No PDF
15 100 NIXIMIXI Not shipped
16 100 NIXIMIXI Not shipped
17 100 NIXIMIXI other
18 100 NIXIMIXI other
19 100 NIXIMIXI other
20 Total For 9 Total Items 900
我想使用Linq或Lambda表达式。 我这样使用但不知道如何把计数列。
listEOMRep = listEOMRep.OrderBy(x => x.client).ThenBy(x => x.MessageType).ToList();
答案 0 :(得分:0)
这不完全符合您的要求,但我认为它完成了工作:
void Main()
{
var source = new List<CustomObject>()
{
new CustomObject() {Id = 1, Count = 100, Client = "MegaTech", MessageType = "Not Shipped" },
new CustomObject() {Id = 2, Count = 250, Client = "NIXIMIXI", MessageType = "Not Shipped" },
new CustomObject() {Id = 3, Count = 45, Client = "NIXIMIXI", MessageType = "No PDF" },
new CustomObject() {Id = 4, Count = 10, Client = "MegaTech", MessageType = "Not Shipped" },
new CustomObject() {Id = 5, Count = 100, Client = "MegaTech", MessageType = "No PDF" },
new CustomObject() {Id = 6, Count = 100, Client = "NIXIMIXI", MessageType = "Not Shipped" },
new CustomObject() {Id = 7, Count = 100, Client = "NIXIMIXI", MessageType = "Not Shipped" },
new CustomObject() {Id = 8, Count = 140, Client = "MegaTech", MessageType = "other" }
};
var result = source.OrderBy(x => x.Client)
.ThenBy (x => x.MessageType)
.GroupBy (s => s.Client)
.Select (s => new {
Key = s.Key,
Object = s,
TotalFor = s.Count(),
TotalItems = s.Sum(x => x.Count)
})
.Dump();
}
public class CustomObject
{
public int Id { get; set; }
public int Count { get; set; }
public string Client { get; set; }
public string MessageType { get; set; }
}
输出: