# LINQ Queries

## Content

LINQ queries demonstrate how to operate and query the Google Analytics 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 **Traffic** table where the **Country** is "**Japan**".

```csharp
var records = from p in context.Traffic
            where p.Country == "Japan"
            select new { p.Country, p.Sessions, p.SocialNetwork, p.Users };
```

**Contains**
Retrieve records from the **Goals** table where the **Name** property contains the substring "**xamarin**".

```csharp
var records = context.Goals.Where(x => x.Name.Contains("xamarin")); 
```

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

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

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

```csharp
var records = (from p in context.Goals
            orderby p.Name descending//Implementing Order By
            select p);
```

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

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

**Group** **by**
Group records from the **Goals** table based on the **Value** property. The groups are then ordered in descending order based on the **Value.**

```csharp
var goalTable = context.Goals.AsEnumerable();
var queryGoal = from b in goalTable
                group b by b.Value into newGroup
                orderby newGroup.Key descending
                select newGroup;
```

**Joins**
Cross-join between the Goals and Traffic tables.

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