locked
Invalid 'where' condition. An entity member is invoking an invalid property or method. Error on LINQ Query. RRS feed

  • Question

  • I am running a linq query against CRM 2011 and I keep getting this error:

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

    My code looks like this:

                try
                {
    
                    string rsmName = "Bob Harrison";
                    var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
                                     join c in gServiceContext.CreateQuery("account") on ((EntityReference) r["accountid"]).Id equals c["accountid"] into opp
                                     from o in opp.DefaultIfEmpty()
                                     where ((EntityReference)r["ownerid"]).Name.Equals(rsmName)
                                     select new
                                                {
                                                    AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
                                                    Account = !o.Contains("name") ? string.Empty : o["name"]
                                                });
    
                    ddlCustomer.DataSource = linqQuery;
                    ddlCustomer.DataValueField = "AccountId";
                    ddlCustomer.DataTextField = "Account";
                    ddlCustomer.DataBind();
                }
                catch (Exception ex)
                {
                }

    Any idea how to fix this?

    Thanks!


    Wednesday, May 30, 2012 2:40 AM

Answers

  • You can't perform operations on the left hand side of a CRM LINQ Query predicate; which is what the following is attempting to do:

    ((EntityReference)r["ownerid"]).Name.Equals(rsmName)

    Each CRM LINQ Query predicate (where sub-clause) must have the following format:

    1. Left hand side is a CRM attribute
    2. Valid operator between LHS and RHS
    3. Right hand side is a variable or literal

    In order to "test" the Name of the Owner in this query, you will need to join the opportunity to systemuser via the ownerid attribute:

    var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
    					join c in gServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals c["accountid"] into opp
    					join u in gServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals u["systemuserid"]
    					from o in opp.DefaultIfEmpty()
    					where u["name"].Equals(rsmName)
    					select new
    					{
    						AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
    						Account = !o.Contains("name") ? string.Empty : o["name"]
    					});
    


    --pogo (pat) @ pogo69.wordpress.com

    Wednesday, May 30, 2012 4:35 AM