LINQ where子句与第二个where一起引发错误 - c#

更新

即使在联系人的名字或姓氏上进行搜索也会导致问题:

var contacts =
            (
                from c in context.ContactSet
                join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
                where m.statuscode.Value == 1
                && ((c.FirstName != null && c.FirstName == searchTerm) || (c.LastName!=null && c.LastName == searchTerm) || (c.FullName != null && c.FullName == searchTerm))  
                orderby c.LastName
                select new
                {
                    ContactId = c.ContactId,
                    FirstName = c.FirstName,
                    LastName = c.LastName,
                    BranchCode = c.py3_BranchArea,
                    Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                    JobTitle = c.JobTitle,
                    Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                    joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                    JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                    Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a"),
                    Title = c.Salutation
                }
            );

哪个尖叫:

'py3_membership'实体不包含Name ='firstname'的属性。

我已经获得了以下代码,可从在线CRM获取一些信息:

        var context = new XrmServiceContext();
    var contacts1 =
        (
            from c in context.ContactSet
            join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
            where m.statuscode.Value == 1

            orderby c.LastName
            select new
            {
                ContactId = c.ContactId,
                FirstName = c.FirstName,
                LastName = c.LastName,
                BranchCode = c.py3_BranchArea,
                Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                JobTitle = c.JobTitle,
                Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
            }
        );

然后,我将其作为数组绑定到数据列表,一切正常。

但是,我希望能够将结果限制为从下拉列表中选择的值,并希望以下各项能够起作用:

        var context = new XrmServiceContext();
    var contacts1 =
        (
            from c in context.ContactSet
            join m in context.py3_membershipSet on c.ContactId equals m.py3_Member.Id
            where m.statuscode.Value == 1 &&
                c.FormattedValues["py3_brancharea"] == ddlBranchTags.SelectedItem.Value


            orderby c.LastName
            select new
            {
                ContactId = c.ContactId,
                FirstName = c.FirstName,
                LastName = c.LastName,
                BranchCode = c.py3_BranchArea,
                Branch = (c.FormattedValues != null && c.FormattedValues.Contains("py3_brancharea") ? c.FormattedValues["py3_brancharea"] : "N/a"),
                JobTitle = c.JobTitle,
                Organisation = (c.ParentCustomerId != null ? c.ParentCustomerId.Name : "N/a"),
                joinedAsCode = c.py3_SOLACEMemberJoinedAs,
                JoinedAs = (c.FormattedValues != null && c.FormattedValues.Contains("py3_solacememberjoinedas") ? c.FormattedValues["py3_solacememberjoinedas"] : "N/a"),
                Expertise = (c.py3_SOLACEMemberAreasofExpertise != null && c.py3_SOLACEMemberAreasofExpertise.Trim() != String.Empty ? c.py3_SOLACEMemberAreasofExpertise : "N/a")
            }
        );

但是,这将引发以下错误:

Invalid 'where' condition. An entity member is invoking an invalid property or method.

即使我对分支标记条件进行了硬编码,而不是对DDL值进行编码,也是如此。
香港专业教育学院还试图对contacts1集进行选择,例如:

var results = contacts1.select(c=> c.BranchTag == ddlBranchTags.SelectedItem.Value

但这引发了同样的错误。

如果我删除branchTag where子句,它将按预期工作。
我认为假设Ive在这方面走得很远是公平的,因此(对于LINQ newb)任何有用的/建设性的指针都将受到赞赏。谢谢。

参考方案

实际上,LINQ-to-CRM可以支持的表达形式相当有限。如果您考虑它在幕后的作用–提取整个LINQ表达式并将其转换为CRM QueryExpression –则更有意义。

基本上,期望不能将任意C#嵌入到您的查询中(即使它可以编译)也是无效的,因为不可能将所有代码都转换为CRM查询。假设查询提供者可能足够聪明,可以确定它可以作为查询提交给CRM的内容,然后确定执行客户端才能获得最终所需结果的代码,但这通常是一个非常棘手的问题-LINQ提供者不尝试解决。

具体在Where子句中,LINQ提供程序扫描表达式中的<attribute name accessor> <simple comparison> <value>基本形式。它了解早期绑定的Codegen属性访问器(entity.Attribute_name),索引器访问(entity["attribute_name"])和entity.GetAttributeValue<T>("attribute_name")访问。这些是您可以在Where子句(或OrderBy子句)的比较左侧使用的唯一内容。在第二个查询中,您访问的是查询提供者不理解/支持的FormattedValues

另一个限制是,单个Where子句只能寻址一个“目标”实体。因此,不支持在单个谓词中同时对“ m”和“ c”同时使用过滤器的情况。我尚不清楚此限制的原因,但事实就是如此。您仍然可以通过将两个条件(在当前查询中以&&联接)分成两个单独的Where子句来完成等效过滤器。

想像使用LINQ-to-Objects一样编写LINQ-to-CRM查询是很自然的,但是这样做通常会令人沮丧。 LINQ-to-CRM受到一定程度的限制,但是其中许多限制是从其基础的CRM CRM SDK查询系统继承而来的。只要您牢记这些限制,它仍然是编写CRM查询的不错的API。

如果要编写查询,要求您执行一些任意代码以过滤/排序/映射结果,则应使用的一般技术是将查询分为两部分:应提交给CRM,以及您的任意代码转换。您编写用于获取CRM数据的数据效率最高的查询,使用ToListToArray之类的方法强制进行查询评估(否则LINQ会被延迟评估),然后在该评估的结果。

LINQ to XML语法 - c#

我有一个简单的POCO类,用于保存从XML文件提取的数据,该XML文件定义如下:public class Demographics { public string FirstName { get; set; } public string LastName { get; set; } public string MiddleName { get; set; …

LINQ to SQL中的“无法识别的表达式节点数组索引linq”异常 - c#

这是我的LINQ,其中我将DateTime类型的字段与当前日期进行了比较- var srs = (from s in dcDistrict.ScheduledReportStatus where s.ReportConfigId.Equals(ConfigId) && s.Status.HasValue && s.Status…

Linq to SQL获取多列 - c#

我正在开发一个asp.net MVC Web应用程序,其中使用Linq to Sql通过jquery和ajax从数据库获取结果。我的模型有以下代码 public IEnumerable<string> getComments(long problemID) { var comment = from c in _objectModel.Commen…

Java中的OrderByDecending(LINQ)等效项 - java

嗨,我是一名使用Java的C#开发人员。问题很简单:我如何才能将下面的c#代码写入Java并仍能正常工作:myCoffeeList.OrderByDescending(x => x.Name?.ToLower()?.Trim() == sender.Text.ToLower()?.Trim())); 我的sender.Text基本上是一个文本框。文本的…

Linq-在嵌套集合中查找元素 - c#

我有一个通用列表-SupportedTypeGroups。每个SupportedTypeGroup都有SupportedTypes属性(SupportedType的通用列表)。如何构造Linq查询以使用所需名称查找SupportedType? 参考方案 var result = SupportedTypeGroups .SelectMany(g => …