我需要帮助从电子邮件地址列表中删除重复项。我需要将代码添加到 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/