C# 具有GroupBy和Count的Linq列表问题
鉴于下面的数据,我试图编写一个LINQ语句,它将按id按ListOfAdmin分组,这样它将只显示一个用户。我还在检查计数是否不是C# 具有GroupBy和Count的Linq列表问题,c#,linq,C#,Linq,鉴于下面的数据,我试图编写一个LINQ语句,它将按id按ListOfAdmin分组,这样它将只显示一个用户。我还在检查计数是否不是0。记录一没有ListOfAdmin,因此Count==0应返回null,但它正在显示ListOfAdmin的记录。阿尔索 一旦我添加了/.GroupBy(f=>f.id)我就得到了错误无法隐式转换类型'System.Collections.Generic.List 所有其他部分工作正常 一旦我添加了ListOfSubscription,但没有ListOfAdmin的
0
。记录一没有ListOfAdmin,因此Count==0
应返回null,但它正在显示ListOfAdmin的记录。阿尔索
一旦我添加了/.GroupBy(f=>f.id)
我就得到了错误无法隐式转换类型'System.Collections.Generic.List
所有其他部分工作正常
一旦我添加了ListOfSubscription,但没有ListOfAdmin的记录,我就会得到错误
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);
}
类订阅
{
公共int SubscriptionId{get;set;}
public int ParentProductId{get;set;}
公共字符串ParentProductName{get;set;}
公共字符串ChildProductName{get;set;}
public int ChildProductId{get;set;}
public int GroupId{get;set;}
公共日期时间?结束日期{get;set;}
public List根据,GrouBy()
返回类型为IEnumerable
的对象
就你而言
(grp2.SelectMany(y=>y.ListOfAdmin).Count())==0?null:grp2.SelectMany(y=>y.ListOfAdmin)
.选择(a=>new AdminViewModel
{
id=a.id,
name=a.name
})
.GroupBy(f=>f.id)
托利斯先生()
返回类型为List
为了使代码正常工作,只需更改SubscriptionViewModel.ListOfAdmin属性的类型
类订阅视图模型
{
[...]
公共列表ListOfAdmin{get;set;}
}
让我们看看确切的错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
很明显:您试图分配的值(类型为List
)不是预期的类型(List
)
您可以通过更改SubscriptionViewModel
来解决此问题,这样ListOfAdmin
将成为@itectori建议的List
但是,我认为这不是您要找的。如果我理解正确,ListOfAdmin
应该包含管理员。如果是这样,只需选择每个I分组的第一项,如下所示:
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(),
当您使用GroupBy()时,您的结果不再是一个列表,而是类似于以int为键的列表(在本例中为id)。因此,您不能重用ListoFamin variableEdit:您的查询实际上返回一个类型为List