0

有什么方法可以缩短这个包含语句?

var query = Context.Businesses
 .Include(b => b.Categories)
 .Include(b => b.Branches.Select(br => br.Address))
 .Include(b => b.Branches.Select(br => br.BranchType))
 .Include(b => b.Branches.Select(br => br.CustomFields))
 .Include(b => b.Branches.Select(br => br.Phones))
 .Include(b => b.Branches.Select(br => br.OpeningTimes.Select(ot => ot.WorkingPeriods)));

我考虑过使用 SPROC,但我不确定它如何知道返回的内容。

那么有没有一种非硬编码的方法可以比它更短呢?也许是一个处理 ? 的所有属性的外部 lambda Branch

4

1 回答 1

1

你可以做一些类似于这个问题的答案的事情。所以在你的情况下,做一个这样的扩展方法:

public static class DataContextExtensions
{
   public static IQueryable<Business> BusinessesComplete(this DataContext context){
         return context.Businesses
             .Include(b => b.Categories)
             .Include(b => b.Branches.Select(br => br.Address))
             .Include(b => b.Branches.Select(br => br.BranchType))
             .Include(b => b.Branches.Select(br => br.CustomFields))
             .Include(b => b.Branches.Select(br => br.Phones))
             .Include(b => b.Branches.Select(br => br.OpeningTimes.Select(ot => ot.WorkingPeriods)));
     }
}

然后像这样使用它:

Business business = context.BusinessesComplete().Where(b => ...etc);
于 2013-04-24T13:17:07.890 回答