我有IQueryable<Anonymous>
Anonymous
{
field = BatchNumber,
list = list of Employees
}
Employees
{
PersonId,
HireDate ....
}
我有一个人的IQueryable
Person
{
PersonId,
Name
}
我想使用linq中的Join方法,而不是查询表达式,结果应该是带有
的IQueryableBatchNumber和Name
(所有员工的batchNumber及其名称)
答案 0 :(得分:1)
如果您的模型如下:
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 = ...;
对于每个Anonymous
,您可以生成BatchNumber
和Name
Employees
属性的新对象,persons
对象PersonId
属性:< / p>
var result = anonymous.SelectMany(a =>
a.Employees.Join(persons,
e => e.PersonId,
p => p.PersonId,
(e, p) => new
{
BatchNumber = a.Field,
Name = p.Name
}));
这会生成包含两个属性的对象集合:BatchNumber
和Name