[]
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".
var records = from p in context.Contracts
where p.Title.Contains("con")
select p;
Count
Count all entities that match a given criterion.
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".
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.
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.
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.
var records = (from p in context.Contracts
select p
).Take(10);
Order By
Sort records by Name in descending order.
var records = (from Accounts in context.Accounts.Take(10)
orderby Accounts.Name descending
select Accounts
).Take(10);