In This Topic
LINQ queries demonstrate how to operate and query the JSON objects wrapped in an Entity Framework data model. Listed below are some examples of LINQ queries supported by the Entity framework.
Contains
Retrieve records from the Books table where the AuthorFirstName contains the letter"A". |
C# |
Copy Code |
var histories = from p in db.Books
where p.AuthorFirstName.Contains("A") //using Contains to display Author names with "a" in their first name
select p; | |
Count
Count all entities that match a given criterion. |
C# |
Copy Code |
var _count = (from p in db.Books
select p).Count();//Count Query based on the number of records selected | |
Select and Filter
Select records from the Books table that belong to a Genre equal to "autobiography". |
C# |
Copy Code |
var histories = from p in db.Books
where p.Genre== "autobiography"
select p; | |
Limit
Select the first 2 records. |
C# |
Copy Code |
var histories = (from p in db.Books
select p).Take(2); //taking 2 histories | |
Order By
Sort records by Title in descending order. |
C# |
Copy Code |
var histories = (from p in db.Books
orderby p.Title descending //Implementing Order By
select p).Take(2); //taking 2 histories | |
Group By
Group records from the Books table based on the Title property. The groups are then ordered in descending order based on the Title. |
C# |
Copy Code |
var booksTable = context.Books.AsEnumerable();
var queryBooks = from b in booksTable
group b by b.Title into newGroup
orderby newGroup.Key descending
select newGroup; | |