c# - Linq - 对每个类进行分组

标签 c# linq

我需要帮助从电子邮件地址列表中删除重复项。我需要将代码添加到 emailaddresses 类中的组电子邮件地址中。由于电话号码不同,Sql 返回重复的电子邮件地址。有什么建议么?谢谢。

这是我的代码: 1.类(class)

public class Student
{
    public string StudentId { get; set; }
    public string Gender { get; set; }  
    public List<EmailAddresses> emailAddresses { get; set; }
}

public class EmailAddresses
{
    public string EmailAddress { get; set; }    
}

<强>2。来自sql查询的数据

StudentId   EmailAddresses  IsPreferred Number  TelephoneType                                                                                               
123456789   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cfc3d0dbc8cdc7e2c5cfc3cbce8cc1cdcf" rel="noreferrer noopener nofollow">[email protected]</a>   FALSE   5556565 Present Evening Phone                                                                                               
123456789   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d505c4f445752587d5a505c5451135e5250" rel="noreferrer noopener nofollow">[email protected]</a>   FALSE   8885566 Permanent Day Phone                                                                                             
123456789   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfa2aebdb6e1a5a0aa8facbabae1aaabba" rel="noreferrer noopener nofollow">[email protected]</a>    TRUE    5556565 Present Evening Phone                                                                                               
123456789   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc919d8e85d2969399bc9f8989d2999889" rel="noreferrer noopener nofollow">[email protected]</a>    TRUE    8885566 Permanent Day Phone                                                                                             
456789123   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb9b59db0a4beb2b1b1b8bab8f3b8b9a8" rel="noreferrer noopener nofollow">[email protected]</a>    TRUE    7513150 Business Day Phone                                                                                              
456789123   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bcb7b6b6b9f6b0b1b4b498bbadf6bdbcad" rel="noreferrer noopener nofollow">[email protected]</a>   TRUE    4123300 Present Day Phone                                                                                               
456789123   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="472328292926692f2e2b2b07243269222332" rel="noreferrer noopener nofollow">[email protected]</a>   FALSE   4123300 Present Day Phone

<强>3。代码

public List<Student> GetStudentData()
{
    List<Student> dataStudent;
    using (IDbConnection connection = RepositoryHelper.OpenConnection())
    {
        dataStudent = connection.Query<dynamic>(
            "mystoredprocedure", 
            commandType: CommandType.StoredProcedure)
                .GroupBy(x => x.StudentId)
                .Select(x => new Student 
                    { 
                        StudentId = x.First().StudentId, 
                        Gender = x.First().Gender,
                        emailAddresses = x.Select(ea => new EmailAddresses 
                            { 
                                EmailAddress = ea.emailAddresses 
                            }).ToList()
                    }).ToList();

        return dataStudent;
    }
}

最佳答案

虽然最好在返回之前过滤掉重复项,但这也会增加代码的复杂性。如果您的结果集很小,则返回数据后进行过滤的内存和 CPU 开销可能会难以轻松、清晰地获得您需要的结果。您可以使用自定义 IEqualityComparer 检查不同的结果,并在包含不同电子邮件地址的每个学生记录上创建新的 emailAddresses 列表。

public List<Student> GetStudentData()
{
    List<Student> dataStudent;
    using (IDbConnection connection = RepositoryHelper.OpenConnection())
    {
        dataStudent = connection.Query<dynamic>(
            "mystoredprocedure", 
            commandType: CommandType.StoredProcedure)
                .GroupBy(x => x.StudentId)
                .Select(x => new Student 
                    { 
                        StudentId = x.First().StudentId, 
                        Gender = x.First().Gender,
                        emailAddresses = x.Select(ea => new EmailAddresses 
                            { 
                                EmailAddress = ea.emailAddresses 
                            }).ToList()
                    }).ToList();

        dataStudent.ForEach(x => x.emailAddresses = x.emailAddresses
            .Distinct(new StudentEmailEqualityComparer()).ToList());

        return dataStudent;
    }
}

public class StudentEmailEqualityComparer : IEqualityComparer<EmailAddresses>
{

    public bool Equals(EmailAddresses x, EmailAddresses y)
    {
        return x.EmailAddress.Equals(y.EmailAddress);
    }

    public int GetHashCode(EmailAddresses obj)
    {
        return obj.EmailAddress.GetHashCode();
    }
}

关于c# - Linq - 对每个类进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052520/

相关文章:

sql - LINQ to SQL 多个 DataContext-s

c# - 是否可以将条件 LINQ 查询组合成每次都运行的查询?

c# - .NET 新手套接字问题

c# - 获取嵌套实体的数量

C# 线程 - 锁 - 如何检查锁的发生

linq - 在 Linq To SQL 中使用 XQuery?

c# - 使用 Xml Schema Inference 和 XDocument 以编程方式从 XML 创建 XSD schema

c# - 为什么此 C# 代码会使进程崩溃?

c# - 从 Nuget 包中自动提取 native 和托管 DLL

c# - 如何使用 LINQ 读取包含 xincluded 文件的 XML?