user482375
user482375

Reputation:

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

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!

Upvotes: 3

Views: 3894

Answers (2)

Peter Majeed
Peter Majeed

Reputation: 5352

The problem isn't the Linq provider, but an issue with EntityReferences. There's unforunately no documentation on it, but my best guess is that since there's no constructor for EntityReference that takes a name, it is ordinarily null unless it's otherwise populated.

But anyway, this can still be done via the same way it would be done via - by querying the SystemUser table. See below (you'll have to translate to late-binding).

var linqQuery = from r in gServiceContext.OpportunitySet
                join c in gServiceContext.AccountSet on r.AccountId.Id equals c.AccountId into opp
                from o in opp.DefaultIfEmpty()
                where o.OwnerId.Id == gServiceContext.SystemUserSet.Single(y => y.FullName.Equals(rsmName)).SystemUserId
                select new
                {
                    AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
                    Account = !o.Contains("name") ? string.Empty : o["name"]
                };

Upvotes: 1

paramosh
paramosh

Reputation: 2258

It looks CRM Linq provider has a limited capabilities and the query is too complex. Try to use FetchXml.

Upvotes: 2

Related Questions