【问题标题】:EF.Property called with wrong property name. - Nested Where Clause - EF Core使用错误的属性名称调用 EF.Property。 - 嵌套 Where 子句 - EF Core
【发布时间】:2020-08-23 17:43:48
【问题描述】:
List<Invitation> a = dbContext.invitations
                .Include(i=>i.ArticleReviews)
                .Include("Article.AuthorsAndOpposedReviewers.User")
                .Include(i=>i.User)
                .Where(i=>i.Article.AuthorsAndOpposedReviewers.Where(a=>a.User.Email == User.Identity.Name && a.role == "coAuthor") != null)
                .Where(i=>i.status == "reviewSubmitted").ToList();

在上面的查询中,Article.AuthorsAndOpposedReviewers 是一个列表,因此要加载相关对象,我使用硬编码字符串 Article.AuthorsAndOpposedReviewers.User

现在,当我使用 where inside another where 为当前登录用户加载数据时,会出现错误 EF.Property called with wrong property name.

抛出错误的语句

.Where(i=&gt;i.Article.AuthorsAndOpposedReviewers.Where(a=&gt;a.User.Email == User.Identity.Name &amp;&amp; a.role == "coAuthor") != null)

【问题讨论】:

    标签: asp.net-core .net-core entity-framework-core asp.net-core-mvc ef-core-3.0


    【解决方案1】:

    不要使用嵌套的where,而是使用这样的任何子句

    .Where(i=&gt;i.Article.AuthorsAndOpposedReviewers.Any(a=&gt;a.User.Email == User.Identity.Name &amp;&amp; a.role == "coAuthor"))

    如果任何子查询值满足条件,则 ANY 运算符返回 true

    【讨论】:

    • 您能解释一下为什么这种方法是更好的选择吗?
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多