We have made the data a bit complex by have one customer and multiple orders , in other words we have one as to many relationship.
![2.JPG]()
So let's make two classes one is the customer class aggregated with a collection of addresses class. Below is how the class structure will look like to accommodate the one as to many relationships of customer and multiple addresses.
![3.JPG]()
The multiple addresses are the array collection aggregated inside the customer class. So below is the code snippet which is loading the customer and address collections with hard coded data provided in the above table. Currently its hardcoded but this can be loaded from database or some other source also.
clsCustomer[] objCustomer = new clsCustomer[]
{
new clsCustomer
{
CustomerName="Khadak",customerCode="001",City="Mumbai",Orders = new clsOrder[]
{
new clsOrder{ProductName="Shirt"}, new clsOrder{ProductName="Socks"}}},
new clsCustomer{CustomerName="Shiv",customerCode="002",City="Delhi",Orders = new clsOrder[]{
new clsOrder{ProductName="Pants"}}},
new clsCustomer{CustomerName="Raju",customerCode="003",City="Mumbai",Orders = new clsOrder[]{
new clsOrder{ProductName="Socks"}}},
new clsCustomer{CustomerName="Shaam",customerCode="004",City="Delhi",Orders = new clsOrder[]{
new clsOrder{ProductName="Shoes"}}}};
A basic LINQ query looks like something as shown below. Its start with the verb from followed by the data type and object i.e. 'clsCustomer' and 'obj' object. 'objCustomer' is the collection which has customer and addresses which we have loaded in the top section. 'select obj' specifies that we need all the values.
from clsCustomer obj in objCustomer select obj
Below figure shows in the right hand side the query in LINQ. In the left hand side we loop through the object collection.
![4.JPG]()
We have made a simple project which demonstrates the basic LINQ query; you can download the same see how it works actually. Below figure shows the execution of the simple query.
![5.JPG]()
We need to put the where clause before the 'select' keyword.
return from clsCustomer Obj in objCustomer where Obj.customerCode == "001" select Obj;
Below figure shows the where clause in action.
![6.JPG]()
Below is the LINQ code snippet for creating joins between object collections. In this case we are creating a join on customer and orders. If you remember the order collection was contained in the customer class.
return from clsCustomer ObjCust in objCustomer from clsOrder ObjOrder in ObjCust.Orders select ObjCust;
Below is the result of how LINQ join query looks like.
![7.JPG]()
Below is the code snippet which shows how group by query is written using LINQ. You can see we have created first a temp variable i.e. 'GroupTemp' and then we have used the 'Select' clause to return the same.
var GroupCustomers = from ObjCust in objCustomer group ObjCust
by ObjCust.City
into GroupTemp select
new {GroupTemp.Key,GroupTemp};
Below image shows group by in action.
![8.JPG]()
Order by in LINQ is pretty simple. We just need to insert order by before the 'Select' query.
return from clsCustomer ObjCust in objCustomer orderby ObjCust.City select ObjCust;
Below figure shows how we have ordered on the city name.
![9.JPG]()
Source Code