foreachループを使用してコレクションを反復処理するメソッドに値を渡します。ループでは、Include
ステートメントがエンティティフレームワークから熱心なロードに使用されます。これは私が渡すものです:
var exp = new Collection<Expression<Func<Foo,object>>>();
私がこれを使うとき、それはなぜですか:
exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Position)));
exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Bank)));
従業員、役職、銀行はすべてName
、異なるフィールド間で名前を混乱させるフィールドを持っていますか?のように、銀行と役職は両方とも、名前フィールドに従業員の名前があります。より明確にするために、何らかの理由で
データベースの場合:
Employee.Name = "Jon";
Employee.Bank.Name = "World Bank";
Employee.Position.Name = "CEO";
からのデータ.Include
:
Employee.Bank.Name == "Jon" //true
Employee.Position.Name == "Jon" //true
expを受け入れるメソッド内の追加情報
DbSet<Foo> dbSet = context.Set<Foo>();
IQueryable<Foo> query = dbSet;
if (exp != null)
{
foreach (var incProp in exp)
{
query = query.Include(incProp);
}
}
コードで何か間違ったことをしていますか?
編集
public class Foo
{
public int FooId { get; set; }
public virtual List<Bar> Bars { get; set; }
}
public class Bar
{
public int BarId { get; set; }
public virtual Foo Foo { get; set; }
public int FooId { get; set; }
public virtual List<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public int BarId { get; set; }
public virtual Bar Bar { get; set; }
public int BankId { get; set; }
public virtual Bank Bank { get; set; }
public int PositionId { get; set; }
public virtual Position Position { get; set; }
public string Name { get; set; }
}
public class Bank
{
public int BankId { get; set; }
public string Name { get; set; }
}
public class Position
{
public int PositionId { get; set; }
public string Name { get; set; }
}