我收集了2种不同的对象。
我试图根据字符串属性做一个.Intersect ......
public class EmployeeDto
{
public string SSN { get; set; }
public DateTime HireDate { get; set; }
}
public class PersonDto
{
public string SocialSecurityNumber { get; set; }
public string Name { get; set; }
}
尝试:
ICollection<EmployeeDto> empsColl = new List<EmployeeDto>();
empsColl.Add(new EmployeeDto() { SSN = "222-22-2222", HireDate = DateTime.Now });
empsColl.Add(new EmployeeDto() { SSN = "333-33-3333", HireDate = DateTime.Now });
empsColl.Add(new EmployeeDto() { SSN = "444-44-4444", HireDate = DateTime.Now });
empsColl.Add(new EmployeeDto() { SSN = "555-55-5555", HireDate = DateTime.Now });
ICollection<PersonDto> pers = new List<PersonDto>();
pers.Add(new PersonDto() { SocialSecurityNumber = "333-33-3333", Name = "John" });
pers.Add(new PersonDto() { SocialSecurityNumber = "444-44-4444", Name = "Mary" });
ICollection<EmployeeDto> overlap1 = empsColl.Where(api => api.SSN.Any(val => val.Equals(pers.Any(cl => cl.SocialSecurityNumber.Equals(val))))).ToList();
ICollection<EmployeeDto> overlap2 = empsColl.Where(api => api.SSN.Equals(pers.Any(per => per.SocialSecurityNumber.Equals(api.SSN, StringComparison.OrdinalIgnoreCase)))).ToList();
foreach (EmployeeDto emp in overlap1)
{
Console.WriteLine(emp.SSN);
}
所需的结果是ICollection<EmployeeDto>
,当然包含333-33-3333和444-44-4444的EmployeeDto对象。
感谢。
答案 0 :(得分:2)
以下是如何做到的:
public Dimension getSize()
答案 1 :(得分:1)
我认为这样做会
empsColl.Where(ec => pers.Select(p => p.SocialSecurityNumber).Contains(ec.SSN)));
答案 2 :(得分:1)
这应该有效
var result = empsColl.Where(x => pers.Any(y=> string.Compare(y.SocialSecurityNumber, x.SSN) == 0));