首页 > 解决方案 > GroupBy 和 Count 的 Linq 列表问题

问题描述

鉴于下面的数据,我正在尝试编写一个 LINQ 语句,该语句将按 id 按 ListOfAdmin 分组,因此它只会显示一个用户。我也在检查计数不是0。记录一没有 ListOfAdmin 因此Count==0应该返回 null 但它正在显示 ListOfAdmin 的记录。另外,一旦我添加//.GroupBy(f => f.id),我就会收到错误Cannot implicitly convert type 'System.Collections.Generic.List

所有其他部分都工作正常。

一旦我添加没有 ListOfAdmin 记录的 ListOfSubscription,我就会收到错误消息

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);
}

更新:使用 List<IGrouping<int, AdminViewModel>>我得到重复。 在此处输入图像描述

标签: c#linq

解决方案


根据文档GrouBy()返回一个类型的对象IEnumerable<IGrouping<TKey,TElement>>

在你的情况下,

(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()

返回一个类型的对象List<IGrouping<int, AdminViewModel>>

为了让您的代码正常工作,您只需更改SubscriptionViewModel.ListOfAdmin属性的类型。

        class SubscriptionViewModel
        {
            [...]
            public List<IGrouping<int, AdminViewModel>> ListOfAdmin { get; set; }
        }

推荐阅读