[英]Linq List problems with GroupBy and Count
Given the data below, I am trying to write a LINQ statement that will group by ListOfAdmin by id so it will only display one user.鉴于下面的数据,我正在尝试编写一个 LINQ 语句,该语句将按 id 按 ListOfAdmin 分组,因此它只会显示一个用户。 I am also checking that the count is not
0
.我也在检查计数是否为
0
。 The record one has no ListOfAdmin so that Count==0
should return null but it is displaying records for ListOfAdmin .记录一没有 ListOfAdmin ,因此
Count==0
应该返回 null 但它显示 ListOfAdmin 的记录。 Also once I add //.GroupBy(f => f.id)
I get the error Cannot implicitly convert type 'System.Collections.Generic.List
此外,一旦我添加
//.GroupBy(f => f.id)
我得到错误Cannot implicitly convert type 'System.Collections.Generic.List
All of the other section is working fine.所有其他部分都工作正常。
Once I add ListOfSubscription with no record for ListOfAdmin I get the error一旦我添加了没有 ListOfAdmin 记录的 ListOfSubscription,我就会收到错误消息
LINQPad Example LINQPad 示例
class Subscription
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
public int GroupId { get; set; }
public DateTime? EndDate { get; set; }
public List<admin> ListOfAdmin { get; set; }
}
class SubscriptionViewModel
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string SubscriptionIds { get; set; }
public int GroupId { get; set; }
public List<SubscriptionChildViewModel> ListOfSubscriptionChild { get; set; }
public List<AdminViewModel> ListOfAdmin { get; set; }
}
class SubscriptionChildViewModel
{
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
class AdminViewModel
{
public int id { get; set; }
public string name { get; set; }
}
class admin
{
public int id { get; set; }
public string name { get; set; }
}
void Main()
{
List<Subscription> ListOfSubscription = new List<Subscription>();
List<admin> ListOfAdmin = new List<admin>();
ListOfSubscription.Add(new Subscription() { SubscriptionId = 1, ParentProductId = 4, ChildProductId = 4, ParentProductName = "Product 1", ChildProductName = "Product 1", GroupId = 362,ListOfAdmin= ListOfAdmin });
ListOfAdmin.Clear();
ListOfAdmin.Add(new admin() { id = 1, name= "Mike"});
ListOfAdmin.Add(new admin() { id = 2, name = "Bill" });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 114, ChildProductId = 1, ParentProductName = "Product 2", ChildProductName = "Product 3", GroupId = 1, ListOfAdmin= ListOfAdmin });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 114, ChildProductId = 2, ParentProductName = "Product 2", ChildProductName = "Product 4", GroupId = 1, ListOfAdmin = ListOfAdmin });
var groupedSubscriptions = ListOfSubscription.GroupBy(u => u.GroupId);
var result = groupedSubscriptions.Select(grp1 => new
{
GroupId = grp1.Key,
Subscriptions = grp1.GroupBy(subscr => new
{
subscr.ParentProductId,
subscr.ParentProductName,
//subscr.ListOfAdmin
})
.Select(grp2 => new SubscriptionViewModel
{
GroupId = grp1.Key,
ParentProductId = grp2.Key.ParentProductId,
ParentProductName = grp2.Key.ParentProductName,
SubscriptionIds = string.Join(",", grp2.Select(y => y.SubscriptionId)),
ListOfSubscriptionChild = grp2
.Where(subsc => subsc.ChildProductId != grp2.Key.ParentProductId)
.Select(subsc => new SubscriptionChildViewModel
{
ChildProductId = subsc.ChildProductId,
ChildProductName = subsc.ChildProductName
})
.ToList(),
ListOfAdmin = (grp2.SelectMany(y => y.ListOfAdmin).Count()) == 0 ? null : grp2.SelectMany(y => y.ListOfAdmin)
.Select(a => new AdminViewModel
{
id = a.id,
name = a.name
})
//.GroupBy(f => f.id)
.ToList(),
})
});
var x = result.SelectMany((s => s.Subscriptions));
Console.Write(x);
}
Updated: using List<IGrouping<int, AdminViewModel>>
I am getting dups.更新:使用
List<IGrouping<int, AdminViewModel>>
我得到了重复。
According to the documentation , GrouBy()
returns an object of type IEnumerable<IGrouping<TKey,TElement>>
.根据文档,
GrouBy()
返回类型为IEnumerable<IGrouping<TKey,TElement>>
。
In your case,在你的情况下,
(grp2.SelectMany(y => y.ListOfAdmin).Count()) == 0 ? null : grp2.SelectMany(y => y.ListOfAdmin)
.Select(a => new AdminViewModel
{
id = a.id,
name = a.name
})
.GroupBy(f => f.id)
.ToList()
return an object of type List<IGrouping<int, AdminViewModel>>
返回一个
List<IGrouping<int, AdminViewModel>>
类型的对象
In order for your code to work, you just need to change the type of the SubscriptionViewModel.ListOfAdmin attribute.为了让您的代码正常工作,您只需更改SubscriptionViewModel.ListOfAdmin属性的类型。
class SubscriptionViewModel
{
[...]
public List<IGrouping<int, AdminViewModel>> ListOfAdmin { get; set; }
}
Let's take a look at the exact error: Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IGrouping<int,Program.AdminViewModel>>' to 'System.Collections.Generic.List<Program.AdminViewModel>'
.让我们来看看确切的错误:
Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IGrouping<int,Program.AdminViewModel>>' to 'System.Collections.Generic.List<Program.AdminViewModel>'
。
It is pretty clear: you are trying to assign a value (of type List<IGrouping<int,Program.AdminViewModel>>
) that is not of the expected type ( List<Program.AdminViewModel>
).很明显:您正在尝试分配一个不是预期类型(
List<Program.AdminViewModel>
)的值(类型List<IGrouping<int,Program.AdminViewModel>>
)。
You can remedy this by changing SubscriptionViewModel
, so that ListOfAdmin
will be List<IGrouping<int, AdminViewModel>>
, as @itectori suggested.您可以通过更改
SubscriptionViewModel
来解决这个问题,这样ListOfAdmin
将是List<IGrouping<int, AdminViewModel>>
,正如@itectori 建议的那样。
However, I don't think that's what you're looking for.但是,我认为这不是您要查找的内容。 If I understand correctly,
ListOfAdmin
should contain, well, the admins.如果我理解正确,
ListOfAdmin
应该包含管理员。 If that is the case, simply select the first item of every IGrouping
, like this:如果是这种情况,只需选择每个
IGrouping
的第一项,如下所示:
ListOfAdmin = (grp2.SelectMany(y => y.ListOfAdmin).Count()) == 0 ? null : grp2.SelectMany(y => y.ListOfAdmin)
.Select(a => new AdminViewModel
{
id = a.id,
name = a.name
})
.GroupBy(f => f.id)
.Select(g => g.First())
.ToList(),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.