LINQ中Where语句中的错误

时间:2012-05-30 16:47:50

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

出于某种原因,在我所说的地方"名字"机会实体中不存在。但它是为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"]
                             });

2 个答案:

答案 0 :(得分:11)

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

  

where 子句将过滤器应用于结果,通常使用a   布尔表达式。过滤器指定要排除的元素   来自源序列。每个 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"]
                };

答案 1 :(得分:2)

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

from r in gServiceContext.CreateQuery("opportunity")

u["firstname"]

更改您的结束地点

r["firstname"].Equals(rsmFirstName)