Entity Navigation Property IQueryable cannot be translated into a store expression

前端 未结 2 1756
醉酒成梦
醉酒成梦 2021-01-28 08:40

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 fi

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 09:24

    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()
    });
    

提交回复
热议问题