Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Use Dynamic Entity with LINQ Queries and the CrmDataContext

Use Dynamic Entity with LINQ Queries and the CrmDataContext

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The CrmDataContext class provides a dynamic, property-bag style of coding as an alternative to the static, strongly typed XrmDataContext class, generated using the CrmSvcUtil tool. When you use CrmDataContext, you can use various helper methods to dynamically supply the type information that the generated code normally provides. Use the GetEntities(string) method to retrieve an entity set of dynamic entities (IQueryable<IDynamicCrmEntity>). Use the IDynamicCrmEntity.GetPropertyValue<T>(string) and IDynamicCrmEntity.SetPropertyValue(string) methods to access the desired attributes.

The entity and attribute names used as input to these helper methods are the schema names defined in the Microsoft Dynamics CRM metadata, for example, "contact" and "contactid".

Examples

The following examples show how to perform a LINQ query with dynamic entities.

// In a basic query, the identifier 'contact' is an IDynamicCrmEntity.
var contacts1 =
  from contact in crm.GetEntities("contact")
  select contact;

foreach (var contact in contacts1)
{
  // Call the 'GetPropertyValue<T>' method to dynamically retrieve attribute values.
  Console.Write("{0}, ", contact.GetPropertyValue<string>("fullname"));
}

// Project to an anonymous type.
var contacts2 =
  from contact in crm.GetEntities("contact")
  select new { ModifiedOn = contact.GetPropertyValue<DateTime?>("modifiedon"),   LastName = contact.GetPropertyValue<string>("lastname") };

foreach (var contact in contacts2)
{
  Console.Write("{0} {1}, ", contact.ModifiedOn, contact.LastName);
}

// Use an order-by clause.
var contacts3 =
  from contact in crm.GetEntities("contact")
  orderby contact.GetPropertyValue<string>("lastname") descending,  contact.GetPropertyValue<string>("firstname") ascending
  select contact.GetPropertyValue<string>("fullname");

foreach (var contact in contacts3)
{
  Console.Write("{0}, ", contact);
}

// Use a where clause.
var contacts4 =
  from contact in crm.GetEntities("contact")
  where contact.GetPropertyValue<string>("lastname") == "Brown"
  select contact.GetPropertyValue<string>("fullname");

foreach (var contact in contacts4)
{
  Console.Write("{0}, ", contact);
}

var contacts5 =
  from contact in crm.GetEntities("contact")
  where contact.GetPropertyValue<DateTime?>("birthdate") > new DateTime(1970, 1, 1)
    && contact.GetPropertyValue<bool>("creditonhold")
  select contact.GetPropertyValue<string>("fullname");

foreach (var contact in contacts5)
{
  Console.Write("{0}, ", contact);
}

// Use a string filter.
var contacts6 =
  from contact in crm.GetEntities("contact")
  where contact.GetPropertyValue<string>("lastname").StartsWith("A")
    || contact.GetPropertyValue<string>("lastname").EndsWith("Z")
    || contact.GetPropertyValue<string>("lastname").Contains("to")
    || contact.GetPropertyValue<string>("lastname").Equals("AtoZ")
  select contact.GetPropertyValue<string>("fullname");

foreach (var contact in contacts6)
{
  Console.Write("{0}, ", contact);
}

// Use a join to find the contacts that have an associated parent customer.
var contacts7 =
  from contact in crm.GetEntities("contact")
  join parentCustomer in crm.GetEntities("account")
    on contact.GetPropertyValue<Guid>("parentcustomerid") equals   parentCustomer.GetPropertyValue<Guid>("accountid")
  select contact.GetPropertyValue<string>("fullname");

foreach (var contact in contacts7)
{
  Console.Write("{0}, ", contact);
}

// Find the marketing lists that a contact is subscribed to.
var marketingLists1 =
  from marketingList in crm.GetEntities("list")
  // Join through the list-to-contact relationship.
  join mlm in crm.GetEntities("listmember")
    on marketingList.GetPropertyValue<Guid>("listid") equals   mlm.GetPropertyValue<Guid>("listid")
  join contact in crm.GetEntities("contact")
    on mlm.GetPropertyValue<Guid>("entityid") equals  contact.GetPropertyValue<Guid>("contactid")
  where contact.GetPropertyValue<string>("emailaddress1") ==   "allison.brown@contoso.com"
  select marketingList.GetPropertyValue<string>("listname");

foreach (var marketingList in marketingLists1)
{
  Console.Write("{0}, ", marketingList);
}

Working with entity lookup attributes

The following sample demonstrates how to use a lookup.

Example

// Use a join to, find the parent account of a contact.
var account1 = (
  from parentCustomer in crm.GetEntities("account")
  join contact in crm.GetEntities("contact")
    on parentCustomer.GetPropertyValue<Guid>("accountid") equals contact.GetPropertyValue<Guid>("parentcustomerid")
  where contact.GetPropertyValue<string>("emailaddress1") ==  "allison.brown@contoso.com"
  select parentCustomer.GetPropertyValue<string>("name")).First();

// Use a join to find the primary contact of an account.
var contact1 = (
  from contact in crm.GetEntities("contact")
  join account in crm.GetEntities("account")
    on contact.GetPropertyValue<Guid>("contactid") equals account.GetPropertyValue<Guid>("primarycontactid")
  where account.GetPropertyValue<string>("name") == "Fabrikam"
  select contact.GetPropertyValue<string>("fullname")).First();

// Use a join to find the account with the specified primary contact.
var account2 = (
  from account in crm.GetEntities("account")
  join contact in crm.GetEntities("contact")
    on account.GetPropertyValue<Guid>("primarycontactid") equals contact.GetPropertyValue<Guid>("contactid")
  where contact.GetPropertyValue<string>("emailaddress1") == "allison.brown@contoso.com"
  select account.GetPropertyValue<string>("name")).First();

Filter against multiple entities

When you want to filter (using a where clause) against multiple entities within the same query, it is important to isolate each entity (produced by a from or join) into individual where clauses. This is because the underlying Microsoft Dynamics CRM query provider groups filter conditions under a common entity, isolated from other entities. Multiple where clauses form the equivalent of an and (&&)-based compound condition.

Example

The following sample shows how to use a filter data from multiple entities.

// Use a where clause with a join.
var calls1 =
  from call in crm.GetEntities("phonecall")
  join contact in crm.GetEntities("contact")
  on call.GetPropertyValue<Guid>("regardingobjectid") equals contact.GetPropertyValue<Guid>("contactid")

// Each entity needs to be separated into distinct 'where' clauses and each 'where' 
// clause can only specify a single entity.

// Use a where clause for the 'contact' entity.
  where contact.GetPropertyValue<string>("emailaddress1") == "allison.brown@contoso.com" 

// Use a where clause for the 'call' entity.
  where call.GetPropertyValue<string>("subject").StartsWith("Support") && call.GetPropertyValue<string>("subject").EndsWith("Call") 
  select call.GetPropertyValue<string>("subject");

foreach (var call in calls1)
{
  Console.Write("{0}, ", call);
}

Using Indexer Syntax

An alternative to calling the GetPropertyValue method is to use indexer syntax. The Value accessor returns the raw CLR value as an object reference that can be casted to the appropriate data type.

Example

The following sample shows how to use the indexer.

var marketingLists2 =
  from marketingList in crm.GetEntities("list")
  join mlm in crm.GetEntities("listmember") // Join through the list-to-contact relationship.
  on marketingList["listid"] equals mlm["listid"]
  join contact in crm.GetEntities("contact")
  on mlm["entityid"] equals contact["contactid"]
  where (string) contact["emailaddress1"].Value == "allison.brown@contoso.com"
  select new { ListName = (string) marketingList["listname"].Value, Cost = (decimal?) marketingList["cost"].Value };

foreach (var marketingList in marketingLists2)
{
  Console.Write("{0} {1}, ", marketingList.ListName, marketingList.Cost);
}

See Also

Concepts


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2015 Microsoft