Use LINQ to Construct a Query

The .NET Language-Integrated Query (LINQ) query provider in MicrosoftDynamics CRM 2011 and Microsoft Dynamics CRM Online uses standard LINQ syntax.
The first step in creating a LINQ query is to identify the relevant entity types
and the relationships between them. You can then specify the data source and the
other query parameters.The from clause is used to return a single “root”
entity. The query provider can only return entities of a single entity type. The orderby
and select clauses must reference this root entity. You can
use joinclauses to add entities with a relationship to the “root” entity.

In This Topic

LINQ Operators
LINQ Limitations

LINQ Operators

All LINQ query expressions have a similar format. The following
table shows the most common clauses in a LINQ query expression when using the Microsoft

Dynamics CRM LINQ query provider.

LINQ Operator

Description

Obtain a data source (the from clause) When using the generated service context and early binding, use theIQueryable entity set, such as AccountSet,
in the generatedcontext.When not using the generated context, the CreateQuery
method on theorganization service context object gives you access to Microsoft Dynamics CRMentities.
Using the generated
service context:

C#

var query1 = from c in context.ContactSet;
 select c;

Using the CreateQuery method:

C#

var query1 = from c in context.CreateQuery;(); 
select c;
join clause The join
clause represents an inner join. You use the clause to workwith two or more entities that can be joined with a common attributevalue.

C#

from c in context.ContactSet; 
join a in context.AccountSet on c.ContactId equals a.PrimaryContactId.Id
Filtering (the where
clause)
The where
clause applies a filter to the results, often using aBoolean expression. The filter specifies which elements to exclude from thesource sequence. Each where clause can only contain
conditions against asingle entity type. A composite condition involving multiple entities is notvalid. Instead, each entity should be filtered in separate whereclauses.

C#

from a in context.AccountSet
 where (a.Name.StartsWith("Contoso") && a.Address1_StateOrProvince == "WA")
orderby The orderby
operator puts the returned query attributes in a specifiedorder.

C#

var query1 = from c in context.CreateQuery<Contact>()
   orderby c.FullName ascending
  select c;
foreach ( var q in query1)
  {
  Console.WriteLine(q.FirstName + " " + q.LastName);
   }
select clause The select
clause defines the form of the data returned. The clause
creates a column set based on the query expression results. You can also definean instance of a new object to work with. The newly created object using theselectclause is not created on the server, but is a localinstance.

C#

select new Contact
{
   ContactId = c.ContactId,
  FirstName = c.FirstName,
   LastName = c.LastName,
   Address1_Telephone1 = c.Address1_Telephone1
 };

LINQ
Limitations

The LINQ query provider supports a subset of the LINQ operators.
Not all

conditions that can be expressed in LINQ are supported. The following table

shows some of the limitations of the basic LINQ operators.

LINQ Operator

Limitations

join Represents an inner
join. You cannot perform outer joins.
from Supports one from
clause per query.
where The left side of the
clause must be an attribute name and the right side of
the clause must be a value. You cannot set the left side to a constant. Both thesides of the clause cannot be constants.Supports the String functions Contains,
StartsWith,EndsWith, and Equals.
groupBy Not supported.
FetchXML supports grouping options that are not available withthe LINQ query provider. For more information, see Use FetchXMLAggregation.
orderBy Supports ordering by
entity attributes, such as Contact.FullName.
select Supports anonymous
types, constructors, and initializers.
last The last
operator is not supported.
skip and take Supports skip
and takeusing server-side paging. Theskip value must be greater than or equal to the takevalue.
aggregate Not supported.
FetchXML supports aggregation options that are not availablewith the LINQ query provider. For more information, see Use FetchXMLAggregation.
This entry was posted in MS Dynamic CRM 2011. Bookmark the permalink.

14 Responses to Use LINQ to Construct a Query

  1. Thanks for the share! Very useful info, looking to communicate!

  2. Reclaim PPI says:

    The recommendations you shared here are extremely valuable. It had been such an enjoyable surprise to see that looking forward to me once i woke up today. They are usually to the point and simple to understand. Thanks for your time for the useful ideas you’ve got shared here.

  3. We so appreciate your site post. There are a multitude of ways we could put it to proper use with minimal effort with time and resources. Thank you really for helping have the post answer many issues we have had before now.

  4. aging says:

    I just wanted to inform you how much my spouse and i appreciate every little thing you’ve contributed to help improve the lives of men and women in this subject matter. Through your articles, I have gone through just a newbie to an expert in the area. It’s truly a honor to your initiatives. Thanks

  5. I cannot thank you enough for the posts on your web-site. I know you’d put a lot of time and energy into all of them and hope you know how much I enjoy it. I hope I could do exactly the same for someone else at some point.

  6. The tips you have given here are really valuable. It was such an enjoyable surprise to see that awaiting me when I woke up today. They are usually to the point and easy to learn. Thanks for your time for the valuable ideas you’ve shared in this article.

  7. I merely wanted to inform you how much my spouse and i appreciate everything you’ve shared to help improve lives of men and women in this subject material. Through your articles, I’ve gone through just a novice to a skilled in the area. It’s truly a honor to your initiatives. Thanks

  8. jewelry says:

    Say thanks a lot for your time to have had these things together on this web site. Emily and that i very much appreciated your input through your articles about certain things. I recognize that you have several demands on schedule therefore the fact that a person like you took as much time just like you did to help people just like us through this article is actually highly liked.

  9. Thank you for your time and energy to have decided to put these things together on this web site. Emily and I very much liked your insight through your own articles on certain things. I’m sure that you have many demands on your own program hence the fact that a person like you took as much time as you did to guide people just like us by this article is actually highly treasured.

  10. micro nichos says:

    I won’t be able to thank you adequately for the articles on your website. I know you placed a lot of time and energy into these and hope you know how much I enjoy it. I hope I’ll do the same for another person sometime.

  11. Thanks a lot for your energy to have put these things together on this blog site. Emily and that i very much appreciated your ideas through the articles over certain things. I’m sure that you have a number of demands on program and so the fact that an individual like you took equally as much time as you did to steer people really like us via this article is also highly appreciated.

  12. Great! Thanks for the share!
    Arron

  13. Thanks a lot for your time to have decided to put these things together on this site. Mary and I very much loved your insight through the articles on certain things. I recognize that you have numerous demands on program hence the fact that an individual like you took equally as much time as you did to help people like us through this article is definitely highly liked.

  14. What would all of us do minus the brilliant thoughts you discuss on this web site? Who has the patience to deal with important topics for the sake of common subscribers like me? I actually and my pals are very fortunate to have your web blog among the ones we frequently visit. It is hoped you know how a great deal we enjoy your effort! Best wishes from us all.

Leave a Reply to play flash cool games Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>