[英]I can't find “Include” method with lambda expression in Entity framework?
I am using entity framework and I can't find include method like in this example: 我正在使用实体框架,但找不到此示例中的include方法:
using(ArticleExtractorEntities db=new ArticleExtractorEntities())
{
Preference pref= db.Preferences.Include(
here i find only the function include with the parameter (string path) and I don't find any other overload so how can I use Include with lambda expression? 在这里,我只找到包含参数(字符串路径)的函数,而没有找到其他任何重载,那么如何在lambda表达式中使用Include?
it's not in System.Linq. 它不在System.Linq中。 Add
加
using System.Data.Entity
Update. 更新。 For those looking how it extend your linq query with .Include()
对于那些希望通过.Include()扩展linq查询的人
No longer is it: 不再是:
using System.Data.Entity;
With netcoreapp1.0 it is: 使用netcoreapp1.0,它是:
using Microsoft.EntityFrameworkCore;
You could implement it as shown in this blog post : 您可以实现此博客文章中所示的方法 :
public static class ObjectQueryExtension
{
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> subSelector)
{
return mainQuery.Include(FuncToString(subSelector.Body));
}
private static string FuncToString(Expression selector)
{
switch (selector.NodeType)
{
case ExpressionType.MemberAccess:
return ((selector as MemberExpression).Member as Reflection.PropertyInfo).Name;
case ExpressionType.Call:
var method = selector as MethodCallExpression;
return FuncToString(method.Arguments[0]) + "." + FuncToString(method.Arguments[1]);
case ExpressionType.Quote:
return FuncToString(((selector as UnaryExpression).Operand as LambdaExpression).Body);
}
throw new InvalidOperationException();
}
public static K Include<T, K>(this EntityCollection<T> mainQuery, Expression<Func<T, object>> subSelector)
where T : EntityObject, IEntityWithRelationships
where K : class
{
return null;
}
public static K Include<T, K>(this T mainQuery, Expression<Func<T, object>> subSelector)
where T : EntityObject
where K : class
{
return null;
}
}
Look at the following articles: 请看以下文章:
http://www.thomaslevesque.com/2010/10/03/entity-framework-using-include-with-lambda-expressions/ http://www.thomaslevesque.com/2010/10/03/entity-framework-using-include-with-lambda-expressions/
http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.