# LINQ Queries

## Content

LINQ queries demonstrate how to operate and query the QuickBooks Online objects wrapped in an Entity Framework data model. Listed below are some examples of LINQ queries supported by the Entity framework.
**Contains**
Retrieve all entities that contain "A" in the Note column

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

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

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

**Select and Filter**
Select a record with a specific Id

```csharp
var records = from p in context.Attachables
              where p.Id == "5000000000000504413"
              select p;
```

**Limit**
Select the first 10 records

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


**Order By**
Sort data by Size in descending order.

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

**Group By**
Group records from the **Attachables** table based on the **Category** property. The groups are then ordered in descending order based on the **Category.**

```csharp
var attachablesTable = context.Attachables.AsEnumerable();
var queryAttachables = from b in attachablesTable
                       group b by b.Category into newGroup
                       orderby newGroup.Key descending
                       select newGroup;
```

**Joins**
Cross-join PurchaseOrders and Purchases tables.

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