[英]How to group and select multiple object in linq c#
PersonList = PersonList
.GroupBy(s => s.LastName)
.Select(g => new Person {
LastName = g.Key,
Period = g.Select(p => p.Period),
Time = string.Join(" ", g.Select(v => CheckTime(v.Time, v.Period) + "' " + v.Gtype)) })
.ToList();
I have this linq statement and I am getting this error: 我有此linq语句,但出现此错误:
Cannot implicitly convert type
System.Collection.Generic.Ienumerable<string>
to'string'
.无法将类型
System.Collection.Generic.Ienumerable<string>
隐式转换为'string'
。
Is it possible to select multiple properties of the object of person 是否可以选择人的对象的多个属性
Modify like this 像这样修改
Your code: 您的代码:
Period = g.Select(p => p.Period)
Modified code: 修改后的代码:
Period = g.Select(p => p.Period).FirstOrDefault()
Assuming you want unique groups for lastname and period (and not something like the first period in the name group), you can group on both LastName and Period with .GroupBy(s => new{s.LastName, s.Period})
. 假设您需要唯一的姓氏和句点组(而不是名称组中的第一个句点),则可以使用
.GroupBy(s => new{s.LastName, s.Period})
对姓氏和句点进行.GroupBy(s => new{s.LastName, s.Period})
。 The key will contain both properties: 密钥将包含两个属性:
PersonList = PersonList
.GroupBy(s => new {s.LastName, s.Period})
.Select(g => new Person {
LastName = g.Key.LastName,
Period = g.Key.Period,
Time = string.Join(" ", g.Select(v => CheckTime(v.Time, v.Period) + "' " + v.Gtype)) })
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.