C# 将匿名类型的属性与元素列表联接
我有一个C# 将匿名类型的属性与元素列表联接,c#,linq,c#-4.0,c#-6.0,C#,Linq,C# 4.0,C# 6.0,我有一个IQueryable 我有一个很好的人 Person { PersonId, Name } 我想使用linq中的Join方法,而不是查询表达式,结果应该是具有 批次号和名称 (所有员工的人数及其姓名)如果您有这样的型号: class Anonymous { public string Field { get; set; } public List<Employee> Employees { get; set; } } class Employee {
IQueryable
我有一个很好的人
Person
{
PersonId,
Name
}
我想使用linq中的Join方法,而不是查询表达式,结果应该是具有
批次号和名称
(所有员工的人数及其姓名)如果您有这样的型号:
class Anonymous
{
public string Field { get; set; }
public List<Employee> Employees { get; set; }
}
class Employee
{
public int PersonId { get; set; }
...
}
class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}
IQueryable<Anonymous> anonymous = ...;
IQueryable<Person> persons = ...;
这将生成包含两个属性的对象集合:BatchNumber
和Name
IQueryable<Anonymous> anonymous = ...;
IQueryable<Person> persons = ...;
var result = anonymous.SelectMany(a =>
a.Employees.Join(persons,
e => e.PersonId,
p => p.PersonId,
(e, p) => new
{
BatchNumber = a.Field,
Name = p.Name
}));