You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LINQ中ListOfAdmin分组去重及Count判断问题排查

解决LINQ分组去重与空列表返回null的问题

我来帮你搞定这两个LINQ需求,直接看修改后的代码和关键说明:

你的核心需求是:

  • 按id对ListOfAdmin分组,仅保留唯一用户
  • 当ListOfAdmin的元素数量为0时,返回null而非空列表

之前的问题在于:

  1. 使用GroupBy(f => f.id)后返回的是分组集合,无法直接赋值给List<AdminViewModel>,导致类型错误
  2. 没有正确实现去重逻辑,导致重复数据依然存在

修改后的完整代码

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 }) 
            .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(), 
                // 关键修改:实现去重和null返回逻辑
                ListOfAdmin = grp2.SelectMany(y => y.ListOfAdmin)
                    .Select(a => new AdminViewModel { id = a.id, name = a.name })
                    .GroupBy(f => f.id) // 按id分组去重
                    .Select(g => g.First()) // 取每个分组的第一个元素(同一id的用户信息一致)
                    .ToList() // 转成List<AdminViewModel>
                    .Any() ? // 判断是否有数据
                    grp2.SelectMany(y => y.ListOfAdmin)
                        .Select(a => new AdminViewModel { id = a.id, name = a.name })
                        .GroupBy(f => f.id)
                        .Select(g => g.First())
                        .ToList() : null // 无数据则返回null
            }) 
    }); 
    var x = result.SelectMany((s => s.Subscriptions)); 
    Console.Write(x); 
}

关键修改说明

  1. 去重逻辑实现:
    通过GroupBy(f => f.id).Select(g => g.First())完成按id去重——先按用户id分组,再从每个分组中取第一个元素(因为同一id的用户信息是重复的,取任意一个即可)。这样既解决了类型转换问题,又保证了用户唯一性。

  2. 空列表返回null:
    用Any()替代Count() == 0来判断是否有数据,Any()的性能更优(无需遍历所有元素,找到第一个元素就停止检测)。如果没有admin数据,直接给ListOfAdmin赋值null,否则返回去重后的列表。

  3. 类型错误解决:
    原来的List<IGrouping<int, AdminViewModel>>类型不匹配问题,通过Select(g => g.First())将分组集合转回AdminViewModel序列,再转成List<AdminViewModel>,完美适配SubscriptionViewModel中的属性类型。

现在运行代码就能看到:

  • 第一个Subscription的ListOfAdmin会返回null(因为初始的ListOfAdmin是空的)
  • 后面两个Subscription的ListOfAdmin只会保留Mike和Bill各一次,没有重复数据

内容的提问来源于stack exchange,提问作者user5843610

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 17:17:41