实体导航属性IQueryable不能转换为存储表达式 [英] Entity Navigation Property IQueryable cannot be translated into a store expression

查看:179
本文介绍了实体导航属性IQueryable不能转换为存储表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先使用Entity Framework设计器,我需要从db对象开始创建自定义的Model对象。
我不想使用IEnumerable,因为它会查询太多的字段。



目标是删除此函数内的内部选择:

  using(var db = new dbEntities())
{
var departments = db.departments
。(包括(p => p.employee)
.Where(...)
.Select(p => new CustomDepartmentModel()
{
ID = ID,
Employees = p.employee
.Select(q => new CustomEmployeeModel()
{
ID = q.ID,
Name = q.Name
})。ToList()
});
return departments.ToList();
}

通过使用此功能:

  public static IQueryable< CustomEmployeeModel> ToModel(这个IQueryable< employee> Employee)
{
return Employee.Select(u => new CustomEmployeeModel()
{
ID = u.ID,
Name = u.Name
});
}

但是我总是得到错误:LINQ to Entities不认识方法ToModel。



我没有尝试以这些方式使用它,没有运气:

  Employees = p.employee.AsQueryable()。ToModel()。ToList()// 1 
Employees = db.Entry(p).Collection(f => f.employee).Query ).ToModel()。ToList()// 2

我认为我需要使用像这个:

  public static System.Linq.Expressions.Expression< Func< IQueryable< employee>,IQueryable&&CustomEmployeeModel>> ; ToModel()
{
return p => p.Select(u => new CustomEmployeeModel()
{
ID = u.ID,
Name = u.Name
});
}

但我真的不知道如何使用它。 >

解决方案


我认为我需要使用这样的东西:[snip]但是我真的不能如何使用它。


这正是你需要的,但你也需要 LINQKit 的albahari.com/nutshell/linqkit.aspx。使用它,您的代码将如下所示:

  var toModel = ToModel(); 

var departments2 = db.departments
.AsExpandable()
.Include(p => p.employee)
.Where(p => true)
.Select(p => new CustomDepartmentModel()
{
ID = p.ID,
Employees = toModel.Invoke(p.employee).ToList()
});


im using Entity Framework designer first and I need to create custom Model Objects starting from the db objects. I don't want to use IEnumerable cause it will query too many fields.

The goal is to remove the inner select within this function:

using (var db = new dbEntities())
{
   var departments= db.departments
                      .Include(p => p.employee)
                      .Where(...)
                      .Select(p => new CustomDepartmentModel()
                      {
                         ID = p.ID,
                         Employees = p.employee
                                .Select(q => new CustomEmployeeModel()
                                {
                                    ID = q.ID,
                                    Name= q.Name
                                }).ToList()
                      });
   return departments.ToList();
}

by using this function:

public static IQueryable<CustomEmployeeModel> ToModel(this IQueryable<employee> Employee)
    {
        return Employee.Select(u => new CustomEmployeeModel()
        {
            ID = u.ID,
            Name = u.Name
        });
    }

But I always get the error: "LINQ to Entities does not recognize the method ToModel".

I did try to use it in these ways without luck:

Employees = p.employee.AsQueryable().ToModel().ToList() //1
Employees = db.Entry(p).Collection(f => f.employee).Query().ToModel().ToList() //2

I think that I need to use something like this:

public static System.Linq.Expressions.Expression<Func<IQueryable<employee>, IQueryable<CustomEmployeeModel>>> ToModel()
    {
        return p => p.Select(u => new CustomEmployeeModel()
        {
            ID = u.ID,
            Name = u.Name
        });
    }

but I really can't figure out how to use it.

解决方案

I think that I need to use something like this: [snip] but I really can't figure out how to use it.

That's exactly what you need, but you also need LINQKit to make the query work. With it, your code would look like this:

var toModel = ToModel();

var departments2 = db.departments
    .AsExpandable()
    .Include(p => p.employee)
    .Where(p => true)
    .Select(p => new CustomDepartmentModel()
{
    ID = p.ID,
    Employees = toModel.Invoke(p.employee).ToList()
});

这篇关于实体导航属性IQueryable不能转换为存储表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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