【问题标题】:Lambda expression used inside Include is not valid when using where inside includeInclude 中使用的 Lambda 表达式在使用 where 时无效
【发布时间】:2021-01-13 22:16:18
【问题描述】:
  User user = await _context.users
.Include(u => u.orders.Where(o => o.Date > startDate && o.Date < enddate))
.Where(u => u.UserID == _auth.User.UserID)
.FirstOrDefaultAsync();

所以我在用户和订单表之间有一对多的关系,其中用户模型有一个列表订单。但是我收到一个 500 服务器错误和“在 Include 中使用的 Lambda 表达式无效”

【问题讨论】:

  • Include 用于急切加载所有相关数据,您不能使用任何类型的 LINQ 查询。如果需要过滤数据,需要在第二次查询中获取

标签: asp.net entity-framework .net-core


【解决方案1】:

我通过下载 EF plus 并将查询更改为

解决了这个问题
User user = await _context.users.IncludeFilter(u => u.orders.Where(o => o.Date > startDate && o.Date < enddate)).Where(u => u.UserID == _auth.User.UserID).FirstOrDefaultAsync();
        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2014-11-05
    相关资源
    最近更新 更多