我有两个不同类型的两个列表:
List<LoginTable> loginList = AllLoginTableUser();
List<UserInformation> users = GetAllUserInformation();
他们的专栏:
我需要一个新的列表,它是通过加入LoginId
上面的两个列表而形成的。我不需要重复的值。
我尝试使用此代码,但它会返回多个值,例如LoginId = 100
,然后它会显示100次。
var joined = (from item1 in loginList
join item2 in users on item1.LoginId equals item2.LoginId
orderby item1.LoginId
select new
{
item1.Password,
item2.LoginId,
item2.FullName,
item2.FatherName,
item2.DOB,
item2.JoinDate
}).ToList();
答案 0 :(得分:1)
使用joined.Distint()
方法获取唯一值..
答案 1 :(得分:1)
var joined = (from item1 in loginList
join item2 in users
on item1.LoginId equals item2.LoginId
orderby item1.LoginId
select new UserData
{
Password = item1.Password,
LoginId = item2.LoginId,
FullName = item2.FullName,
FatherName = item2.FatherName,
DOB = item2.DOB,
JoinDate = item2.JoinDate
}).Distinct(new UserDataComparer()).ToList();
public class UserData
{
public string Password {get;set;}
public int LoginId {get;set;}
public string FullName {get;set;}
public string FatherName {get;set;}
public DateTime DOB {get;set;}
public DateTime JoinDate {get;set;}
}
public class UserDataComparer : IEqualityComparer<UserData>
{
public bool Equals(UserData x, UserData y)
{
return x.LoginId == y.LoginId;
}
public int GetHashCode(UserData obj)
{
return obj.LoginId.GetHashCode();
}
}
答案 2 :(得分:0)
Enumerable.Distinct()正是您所寻找的
参见示例here