开发者

Custom LINQ .Include() extension

Given the following pse开发者_如何转开发udo code, how could i call this function with .Include and pass it on to ctx.Table

public class Complicated
{
// fields here
}


public IEnumerable<Complicated> GetComplicatedData<T>()
{

   using(MyDbCtx ctx = new MyDbCtx())
   {
      return ctx.Set<T>().Select(p=> new Complicated { /* fill */ }).ToList();
   }

}


You can't. Your method is doing projection so even if you find the way your include will never be used. If you want to use Include you must use a method this way:

ctx.Table.Include(...).ToList().Select(p => new Complicated { ... });

This example doesn't make much sense because you know what data you need to fill your Complicated item so you can either fill them directly by linq-to-entites without using Include at all or you know upfront what includes will you need to fill your Complicated entity by linq-to-objects.

There is no eager or lazy loading for custom projected classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜