gpt4 book ai didi

c# - LINQ GroupBy 不对结果进行分组

转载 作者:行者123 更新时间:2023-11-30 19:35:23 25 4
gpt4 key购买 nike

我正在尝试按电子邮件属性对常见对象列表进行分组,但没有得到预期的结果。

var users = new List<Profile>
{
new SharePointProfile
{
Id = 1,
LoginName = "Login1",
Title = "Login1 SharePoint",
Email = "Login1@sharepoint.com",
IsSiteAdmin = false
},
new SharePointProfile
{
Id = 2,
LoginName = "Login2",
Title = "Login2 SharePoint",
Email = "Login2@sharepoint.com",
IsSiteAdmin = true
},
new Auth0Profile
{
Email = "Login2@sharepoint.com",
Name = "Login2 SharePoint Auth0"
},
new Auth0Profile
{
Email = "test@test.com",
Name = "Test User Auth0"
}
};

var userGroups = users.GroupBy(m => m.Email)

enter image description here

我希望看到三组:

  • 第 1 组 - Login1@sharepoint.com - 1 个 SharePointProfile 对象
  • 第 2 组 - Login2@sharepoint.com - 1 个 SharePointProfile 对象和 1 个 Auth0Profile 对象
  • 第 3 组 - test@test.com - 1 个 Auth0Profile 对象

我在这里做错了什么?

public abstract class Profile : IEquatable<Profile>
{
public string Name { get; set; }
public string Email { get; set; }

public bool Equals(Profile other)
{
return this.Email == other.Email;
}
}

public class Auth0Profile : Profile
{
public new string Email { get; set; }
public new string Name { get; set; }
public string Connection { get; set; }
public string Password { get; set; }
public bool VerifyEmail { get; set; }
}

public class SharePointProfile : Profile
{
public int Id { get; set; }
public string LoginName { get; set; }
public string Title { get; set; }
public new string Email { get; set; }
public bool IsSiteAdmin { get; set; }
}

最佳答案

问题是您的列表中有 Profile 类型的对象。但是在您对对象的定义中,您为每个对象创建了称为电子邮件的新属性。 linq 查看未使用的属性。

关于c# - LINQ GroupBy 不对结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57777591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com