Answered by:
Linq query return all columns when it shouldn't

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.
- Edited by Marius Solend Tuesday, October 8, 2013 7:41 AM
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.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Proposed as answer by Scott Durow (MVP)MVP, Editor Tuesday, October 8, 2013 7:54 AM
- Marked as answer by Marius Solend Tuesday, October 8, 2013 7:57 AM
Tuesday, October 8, 2013 7:54 AMAnswerer
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.netFollow Me
Rockstar365
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Proposed as answer by Scott Durow (MVP)MVP, Editor Tuesday, October 8, 2013 7:54 AM
- Marked as answer by Marius Solend Tuesday, October 8, 2013 7:57 AM
Tuesday, October 8, 2013 7:54 AMAnswerer -
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