LINQ queries demonstrate how to operate and query the Service Now objects wrapped in an Entity Framework data model. Listed below are some examples of LINQ queries supported by the Entity framework. In the following example, it is used Incident.cs file to map the Incident datatable.
Contains
Retrieve all entities that contain "Server" in the Description column. |
C# |
Copy Code |
var records = context.Incident.Where(x => x.Description.Contains("Server")); | |
Order By
Sort data by Category in ascending order. |
C# |
Copy Code |
var records = (from p in context.Incident
orderby p.Category ascending//Implementing Order By
select p); | |
Count
Count all entities that match a given criterion. |
C# |
Copy Code |
var _count = (from p in context.Incident
select p).Count();//Count Query based on number of records selected | |
Joins
Cross-join Incident and AlmAsset tables. |
C# |
Copy Code |
var records = from b in context.Incident
from e in context.AlmAsset
select new { b, e };//Defining Cross Join | |
Group By
Group records from the Incidents table based on the Category property. |
C# |
Copy Code |
var incidentTable = context.Incident.AsEnumerable();
var queryIncident = from b in incidentTable
group b by b.Category into newGroup
orderby newGroup.Key descending
select newGroup; | |