이것이 올바른 일인지 나는 확신 할 수 없다. 누군가가 그렇지 않은지 말해 줄 것이다.Entity Framework - 하위 쿼리에 포함 하시겠습니까? - 2 부
저는 오늘 저녁에 질문 (Entity Framework - Include in sub query?)을 요청했습니다.이 질문은 아주 잘 대답되어 내 문제를 해결했습니다. 하지만 더 좋은 방법이있을 수 있으므로 질문을 다시하고 조금씩 다르게하겠습니다.
의 내가 3 개 테이블이 있다고 가정 해 봅시다 :
레스토랑 1 ..... M MenuCategory 1 ..... M의 MenuItem 나는 이런 식으로 뭔가 보이는 L2E 쿼리가 :
레스토랑 = 식당 . 포함 (r => r.MenuCategory) .FirstOrDefault (r => r.RestaurantId == resaurantId); 어느 정도까지는 작동하지만 메뉴 범주 만 미리로드합니다.
내가 정말 할 수 있기를 원하는 것은 같은 것입니다 : 작업이 주위에있다 ... r.MenuCategory가 열거
같이 명확하게
Restaurant = context.Restaurant
.Include(r => r.MenuCategory)
.Include(r => r.MenuCategory.MenuItems)
.FirstOrDefault(r => r.RestaurantId == resaurantId);
그러나이 사용할 수없는
context.Restaurant.Include("MenuCategory.MenuItems");
...하지만 강하게 입력되지는 않습니다. 이 질문은 는 강력한 형식의 발견에 관한 것입니다 대답
이 현재의 확장 방법 :
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> path)
{
// Retrieve member path:
List<PropertyInfo> members = new List<PropertyInfo>();
EntityFrameworkHelper.CollectRelationalMembers(path, members);
// Build string path:
StringBuilder sb = new StringBuilder();
string separator = "";
foreach (MemberInfo member in members)
{
sb.Append(separator);
sb.Append(member.Name);
separator = ".";
}
// Apply Include:
return query.Include(sb.ToString());
}
이는의 강력한 형식의 양식을 할 수 있도록 적용 할 수있는 방법 :
context.Restaurant.Include("MenuCategory.MenuItems");
완벽하게, 정확히 내가 한 것. 나는 오늘 밤 그걸로 연극을 할 것이고, 조금만 조정할 수도 있습니다. 고맙습니다! – Paul
방금이 작업을했는데 완벽하게 작동합니다. 새 BlogFetchStrategy()에서 "from"이 아니라 ".IncludeStrategy (new MyStrategy())"와 같이 사용할 수 있도록 확장 메서드를 만들었습니다. ApplyTo (ctx.Blogs) b.Owner == "Alex" 선택 b; " – Paul
그래, 더 좋아 보인다. 나는 항상 나의 접근 방식에 조금 불만했다. 너의 것이 훨씬 좋다. –