locked
Linq query return all columns when it shouldn't RRS feed

  • Question

  • The following code return all attributes on the Entity base class. The generated properties are also set. The properties should be null and the AttributeCollection on the Entity base class should be empty because I'm only asking for the Id:

    var account = (from a in crmContext.AccountSet
                                  select new Account
                                  {
                                      Id = a.Id
                                  }).FirstOrDefault();

    crmContext is the instance of my generated service context.

    I have tried this with late bound as well with the same result. The AttributeCollection has all the attributes:

    var account = (from a in crmContext.CreateQuery("account")
    select new Entity
    {
    Id = a.Id,
    LogicalName = a.LogicalName
    }).FirstOrDefault();

    This suddenly started to happen and I have never encountered this before. The CRM Application is on rollup 14.



    Tuesday, October 8, 2013 7:35 AM

Answers

  • Hi,

    This is just one of those things with the LINQ provider in that if you reference just the Id property it will return all columns. I think it is because it can't lookup the Id attribute name via the Metadata so it returns all attributes.

    You'll need to reference the actual AccountId attribute:

    var account = (from a in crmContext.AccountSet
                                  select new Account
                                  {
                                      AccountId = a.AccountId
                                  }).FirstOrDefault();

    This code will only return the AccountId

    hth,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Tuesday, October 8, 2013 7:54 AM
    Answerer

All replies

  • Hi,

    This is just one of those things with the LINQ provider in that if you reference just the Id property it will return all columns. I think it is because it can't lookup the Id attribute name via the Metadata so it returns all attributes.

    You'll need to reference the actual AccountId attribute:

    var account = (from a in crmContext.AccountSet
                                  select new Account
                                  {
                                      AccountId = a.AccountId
                                  }).FirstOrDefault();

    This code will only return the AccountId

    hth,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Tuesday, October 8, 2013 7:54 AM
    Answerer
  • Thank you.

    Has it always been this way for 2011 or is this some new implementation?
    I'm almost positive that it has worked earlier. 

    Tuesday, October 8, 2013 7:59 AM