【问题标题】:Error within Where statement in LINQLINQ 中的 Where 语句中的错误
【发布时间】:2012-06-04 22:43:09
【问题描述】:

出于某种原因,在我的地方它说机会实体中不存在“名字”。但它是为 SystemUser 实体设置的。知道为什么会感到困惑吗?谢谢!

            var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
                             join c in gServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals c["accountid"]
                             join u in gServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals u["systemuserid"]
                             where r["new_leadstatus"].Equals("100000004") && u["lastname"].Equals(rsmLastName) && u["firstname"].Equals(rsmFirstName)
                             select new
                             {
                                 AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
                                 Account = !r.Contains("name") ? string.Empty : r["name"]
                             });

【问题讨论】:

    标签: linq dynamics-crm dynamics-crm-2011


    【解决方案1】:

    确保将每个 where 子句放在自己的行 per Microsoft guidelines 中。

    where 子句对结果应用过滤器,通常使用 布尔表达式。过滤器指定要排除的元素 从源序列。每个 where 子句只能包含 针对单个实体类型的条件。复合条件 涉及多个实体是无效的。相反,每个实体都应该 在单独的 where 子句中过滤。

    var linqQuery = from r in gServiceContext.CreateQuery("opportunity")
                    join c in gServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals c["accountid"]
                    join u in gServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals u["systemuserid"]
                    where r["new_leadstatus"].Equals("100000004")
                    where u["lastname"].Equals(rsmLastName) && u["firstname"].Equals(rsmFirstName)
                    select new
                    {
                        AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
                        Account = !r.Contains("name") ? string.Empty : r["name"]
                    };
    

    【讨论】:

    • 不知道你可以有两个 where 语句。像魅力一样工作,谢谢!
    • 每天学习新东西。谢谢@彼得
    • 刚刚花了几个小时把我的头撞在墙上。谢谢。
    【解决方案2】:

    您将对机会实体的引用定义为“r”,但尝试从“u”中读取名字

    from r in gServiceContext.CreateQuery("opportunity")
    
    u["firstname"]
    

    把你的 where 结尾改成

    r["firstname"].Equals(rsmFirstName)
    

    【讨论】:

    • 但我想检查来自 systemuser 而不是机会的名字和来自机会的 lnew_leadstatus。
    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多