In This Topic
LINQ queries demonstrate how to operate and query the Dynamics 365 Sales objects wrapped in an Entity Framework data model. Below are some examples of LINQ queries supported by the Entity framework.
Contains
Retrieve records from the Contracts table where the title containdscontains "con". |
C# |
Copy Code |
var records = from p in context.Contracts
where p.Title.Contains("con")
select p; | |
Count
Count all entities that match a given criterion. |
C# |
Copy Code |
var count = (from p in context.Contracts
select p
).Count(); | |
Filter
Select records from the Contracts table that belong to Title equal to "AGR-04". |
C# |
Copy Code |
var records = from p in context.Contracts
where p.Title == "AGR-04"
select p; | |
Group by
Group records from the Accounts table based on the Accountnumber property. The groups are then ordered in descending order based on the Accountnumber. |
C# |
Copy Code |
var accountTable = context.Accounts.AsEnumerable();
var queryAccount = from b in accountTable
group b by b.Accountnumber into newGroup
orderby newGroup.Key descending
select newGroup; | |
Join
Join the tables Contracts and Accounts. |
C# |
Copy Code |
var records = (from c in context.Contracts.Take(10)
from a in context.Accounts.Take(10)
select new { c, a }
).Take(20); | |
Limit
Select the first 10 records. |
C# |
Copy Code |
var records = (from p in context.Contracts
select p
).Take(10); | |
Order By
Sort records by Name in descending order. |
C# |
Copy Code |
var records = (from Accounts in context.Accounts.Take(10)
orderby Accounts.Name descending
select Accounts
).Take(10); | |