LINQ 分组与聚合

本文关键字:LINQ | 更新日期: 2023-09-27 18:34:25

我正在尝试创建检索电子邮件和用户的查询,我想使用聚合函数在一个字段中使用同一电子邮件的所有用户。我的查询在新关键字上出错,因为无法将类型"匿名类型#1"隐式转换为"匿名类型#2"

这是代码。

var Users = from usr in
            (from au in _lldat.aspnet_Users
                     join am in _lldat.aspnet_Membership on au.UserId equals am.UserId
                     select new  { au.UserName, am.LoweredEmail })
            group usr by new { usr.LoweredEmail } into grp 
            select new {
                           uEMail = grp.Key,
                           uName = grp.Aggregate((a, b) => new { LoweredEmail = a.LoweredEmail, UserName = (a.UserName + ", " + b.UserName + ", ") }).UserName 
                       };

看不出我做错了什么。任何帮助将不胜感激。

LINQ 分组与聚合

尝试切换关键字中的参数。它会是这样的:

var Users = from usr in
            (from au in _lldat.aspnet_Users
                     join am in _lldat.aspnet_Membership on au.UserId equals am.UserId
                     select new  { au.UserName, am.LoweredEmail })
            group usr by new { usr.LoweredEmail } into grp 
            select new {
                           uEMail = grp.Key,
                           uName = grp.Aggregate((a, b) => new {UserName = (a.UserName + ", " + b.UserName + ", "), LoweredEmail = a.LoweredEmail }).UserName 
                       };