包含使用Lambda表达式 [英] Include using Lambda expression

查看:115
本文介绍了包含使用Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Include 的基于字符串的重载中,我们指定仅仅通过以正确的顺序指定相关的导航属性来包括一个集合,然后是一个参考一级:

  query.Include(Level1Collection.Level2Reference); 

但是为什么在使用包含使用lambda表达式,我们还必须使用选择语句来指定上述查询:

  query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference))。 

为什么以下工作不会:

  query.Include。(e => e.Level1Collection.Level2Reference)

谢谢

解决方案

因为编译器不认识到上下文已经改变了collection属性的含义从收藏到收藏中的对象。而且由于编译器根据上下文不会改变,所以也不会有intellisense。



当您提供包含一个字符串语句时,它知道它必须使用反射来知道什么属性包括反正,没有类型检查编译。底层方法知道当它在字符串中的collection属性后面看到一个点时,它应该解析集合中对象的属性,而不是集合本身(在这种情况下为Level2Reference)。 p>

或换句话说:这是魔术。 :)


In a string based overload of Include we specify to include a collection and then a reference one level down simply by specifying relevant navigation properties in correct order:

query.Include("Level1Collection.Level2Reference");

But why when using an overload of Include that uses lambda expression, must we also use a Select statement to able to specify the above query:

query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)).

Why wouldn't the following work:

query.Include.(e => e.Level1Collection.Level2Reference)

thank you

解决方案

Because the compiler doesn't recognize that the context has changed the meaning of the collection property from being a collection to being a stand-in for objects in the collection. And since the compiler doesn't change based on context, neither does intellisense.

When you feed Include a string statement, it knows it has to use reflection to know what properties to include anyway and there's no type-checking on compile. The underlying method knows that when it sees a dot after a collection property in the string that it should parse the properties of the objects within the collection for the next referenced property rather than the collection itself ("Level2Reference" in this case).

Or in other words: it's magic. :)

这篇关于包含使用Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆