0

Overview:

I've coded a simple console app that can connect to a Dynamics CRM instance following on from this walk-through.

Now I need to create a second method that will perform a filtered query on the users of that CRM within the C# code using LINQ.

Advanced Find Queries:

I tried this out using the Advanced Find search on Dynamics which equates to the following:

Query 1:

Look for: Users
Primary Email Status -> Equals -> Pending Approval

Query 2:

Look for: Users
Select -> Mailbox 
Status -> Equals -> Inactive

Question:

How can the two queries above be translated to LINQ within the Dynamics SDK C# code?

Matt
  • 4,656
  • 1
  • 22
  • 32
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • 1
    If you have created an Advanced Find you can download the Fetch XML from CRM and use that to create a `FetchExpression`. – Nicknow Feb 08 '16 at 04:58
  • @Nicknow I'm going to take this approach. You can post that suggestion as an answer. – Brian Var Feb 08 '16 at 22:15

3 Answers3

2

You can use pretty much standard LINQ there as Gilgamesh answered.

Be careful because the CRM linq provider only implements a subset of LINQ features ans some operations aren't allowed like:

  • GroupBy
  • More than 1 left outer joins
  • Order by a related column
  • ...

There are many complex examples of the queries you can use here

Jordi
  • 1,460
  • 9
  • 9
1

Have you looked at the msdn article?

The actual LINQ queries will look something like this:

 var users = from systemUser in crmServiceContext.SystemUserSet  
             where systemUser.PrimaryEmailStatus == //some value 
             select systemUser;  
Gilgamesh
  • 680
  • 6
  • 17
  • I used a FetchXML for this case as I can export from the advanced find in CRM. Any idea on casting the query result to a string? http://stackoverflow.com/questions/35280788/how-to-convert-an-entitycollection-to-list – Brian Var Feb 08 '16 at 23:23
0

If you use LinQPad with the Dynamics CRM Driver you can obtain the resulting FetchXml of a Linq query. Other thing you can do is execute your fetchxml string directly in crm without changing it to a linq or lambda (example here)

hope it helps