# LINQ Queries

## Content

LINQ queries demonstrate how to operate and query the Salesforce objects wrapped in an Entity Framework data model. Listed below are some examples of LINQ queries supported by the Entity framework.
**Select and Filter**
Retrieve records from the **Order** table where the **BillingState** is "**Delhi**".

```csharp
var records = from p in context.Order
            where p.BillingState == "Delhi"
            select p;
```

**Contains**
Retrieve records from the **Order** table where the **AccountId** property contains the letter "**A**" in its value.

```csharp
var records = from p in context.Order
            where p.AccountId.Contains("A") //using Contains to display AccountId that have A. 
            select p;
```

**Limit**
Retrieve the first 10 records from the **Order** table.

```csharp
var records = (from p in context.Order
            select p).Take(10); //taking 10 records
```

**Order by**
Retrieve all records from the **Order** table and sort them in **descending** order based on the **AccountId** property.

```csharp
var records = (from p in context.Order
            orderby p.AccountId descending//Implementing Order By
            select p).Take(10); //taking 10 records
```

**Count**
Count all the entities that match a given criterion. 

```csharp
var _count = (from p in context.Order
            select p).Count();//Count Query based on the number of records selected0
```

**Group By**
Group records from the **Order** table based on the **BillingCity** property.

```csharp
 var ordersTable = context.Order.AsEnumerable();
 var queryOrders = from b in ordersTable
                   group b by b.BillingCity into newGroup
                   orderby newGroup.Key descending
                   select newGroup;
```

**Joins**
Cross-join between the **Status** and **BillingState** tables.

```csharp
var records = from b in context.Status
            from e in context.BillingState
            select new { b, e };//Defining Cross Join
```