LINQ分组与聚合

时间:2016-02-12 14:36:15

标签: c# linq aggregate grouping

我正在尝试创建检索电子邮件和用户的查询,我想使用聚合函数让所有用户在一个字段中使用相同的电子邮件。 我的查询在 new 关键字上给出了错误,因为无法将类型'AnonymousType#1'隐式转换为'AnonymousType#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 
                       };

我看不出我做错了什么。任何帮助都将受到重视。

1 个答案:

答案 0 :(得分:0)

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

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    public BitmapImage CurrWall = new BitmapImage();
    BitmapImage CurrentWallpaper
    {
        get { return CurrWall; }
        set { CurrWall = value; OnPropertyChanged("CurrentWallpaper"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}