# LINQ Queries

## Content

LINQ queries demonstrate how to operate and query the CSV objects wrapped in an Entity Framework data model. Below are some examples of LINQ queries supported by the Entity framework.

**Contains** 
Retrieve all records from the **Books** table that contain "**Agriculture**" in their **Industry name**.  
```csharp 
var histories = from p in context.Books
                where p.Industry_name.Contains("Agriculture") //using Contains to display Industry names that contain "Agriculture"
                select p;
``` 

**Count** 
Count all entities that match a given criterion.
```csharp 
var _count = (from p in db.SampleCSV
             select p).Count(); //Count Query based on the number of records selected
``` 

**Select and Filter** 
Select records from the table **SampleCSV** that belong to the **Year** equal to **2011**. 
 ```csharp 
var histories = from p in db.SampleCSV
                where p.Year== 2011
                select p;
``` 

**Limit** 
Select the first 2 records.
```csharp 
var histories = (from p in db.SampleCSV
                select p).Take(2); //taking 2 records
``` 

**Order By** 
Sort records form the table **SampleCSV** by the **Year** in descending order. 
```csharp 
var histories = (from p in db.SampleCSV
                orderby p.Year descending //Implementing Order By
                select p).Take(2); //taking 2 records
```

**Group By** 
Group records from the **SampleCsvs** table based on the **Age** property. The groups are then ordered in descending order based on the **Age**.  
```csharp 
var sampleTable = context.SampleCsvs.AsEnumerable();
var querySample = from b in sampleTable
                  group b by b.Age into newGroup
                  orderby newGroup.Key descending
                  select newGroup;
```
