locked
How to Make Late Bound Linq Queries Efficient and return an Entity object. RRS feed

  • Question

  • Hi,

    If I write a Linq Query using Early Bound format, I see that CRM generates a QueryExpression with only the SELECT new CONTACT fields specified. If I write a Late Bound query and return an anonymous type (i.e. a var), then CRM still only selects the fields specified in the SELECT NEW statement.

    However want I really want is an Entity object that can be passed around rather than a var. I don't want to create my own "Contact" entity - I may as well have use early bound for that.

    I have tried various ways to try and use "SELECT new Entity", but no matter what I try, the CRM Linq evaluator generates a Query Expression with "All Columns = true", which is very inefficient. I have tried various methods to just get the required attributes, but CRM still seems to always set All Columns = true. I guess it can't evaluate the Linq query enough to determine which columns are being referenced.

    Has anybody come up with a way to perform late bound Linq Queries that return a standard Entity object?


    • Edited by Caratacus Monday, January 12, 2015 11:00 AM
    Monday, January 12, 2015 10:58 AM

All replies

  • Hi,

    Did you try below query?

    var contlinqQuery = from a in orgContext.CreateQuery("contact")
                                     select new
                                     {
                                         
                                             FirstName = a["firstname"],
                                             LastName = a["lastname"]
                                        
                                        
                                     };
    
                foreach (var record in contlinqQuery)
                {
                    Console.WriteLine("Contact Name: {0} {1}",
                        record.FirstName, record.LastName);
    
                }

    Thanks!

    Kalim khan

    Monday, January 12, 2015 12:38 PM
  • Kalim, your code generates an anonymous type, not an Entity object.

    I tried this code:

                var query = xrmContext.CreateQuery("contact").Select(new Func<Entity, Entity>(c =>
                {
                    Entity entity = new Entity(c.LogicalName);
                    entity.Id = c.Id;
                    entity["fullname"] = c["fullname"];
                    entity["parentcustomerid"] = c["parentcustomerid"];
                    return entity;
                }
                ));
                List<Entity> contacts = query.ToList();

    The code works, that is I get an entity back just with the columns specified. But if you trace the request you see that CRM actually performs an AllColumns = true query which is very bad, especially for contact. It seems the CRM to Linq projector is faulty since in the RetrieveMultiple query it does actually specify only the named columns. Its just that it incorrectly sets AllColumns=true, when it should say false.


    • Edited by Caratacus Tuesday, January 13, 2015 5:45 PM
    Tuesday, January 13, 2015 5:44 PM