3

When I design a QueryExpression, I've always used the following, hard-coded syntax.

QueryExpression expression = new QueryExpression
{
  EntityName = "account";
  ...
}

In this blog the following syntax based on this enumeration is used.

QueryExpression expression = new QueryExpression
{
  EntityName = EntityName.account.ToString();
  ...
}

I liked it much better but I couldn't find it using intellisense. Then I've found this discussion where it's explained that the syntax is deprecated.

So, what should one use instead of EntityName? I've googled it a bit but can't find anything useful. It seems to me that some kind of enumeration is to be preferred before the hard-coded expression. Is that correct? And if so, how to do that in CRM 2011?

Community
  • 1
  • 1

3 Answers3

3

If you are using early bound classes you could use following syntax also for your custom entities. If not, this will work for all standard entities.

Account.EntityLogicalName
ccellar
  • 10,326
  • 2
  • 38
  • 56
0

And if you are using late bound entities you can use Entity.LogicalName.

James Wood
  • 17,286
  • 4
  • 46
  • 89
  • That's valid once I have the entity instance. What can be used if I'd like to get the logical name of an entity and don't want to type it? Suppose I forgot that *marketing lists'* schema name is simply *list* (or maybe I'm unsure if it's capitalized or not). I can just look it up or give it a try and correct the error but I'd like to learn a more professional method to achieve that. Should I not think about it **that** way (especially since I'm using late bound)? –  Jan 14 '13 at 10:00
  • Well you could query the CRM metadata services to get the name, however this adds a web service call. Also given your example, you then have to remember `marketing list` to get `list`, which is basically the same problem. At some point you will have to remember a reference to the entity in CRM, as the schema name is the one you want it might as well be that. Usually I just hardcode all my entity names either as literal strings or in a readonly variable. – James Wood Jan 14 '13 at 10:23
  • Good point. I was most thinking that it'd be convenient to get intellisense for it. Being lazy, hehe... –  Jan 14 '13 at 10:28
  • 1
    Well if you store all the names in static variables you can use intellisense then. – James Wood Jan 14 '13 at 11:44
0

you can write for example Contact.EntityLogicalName.ToString() but you in this case you must

use early bound classes