Posts

Select Columns From Multiple Tables Using LINQ

From Northwind database, to show all available products it needs to select values from multiple tables because in Products table there are two columns – CategoryID and SupplierID which are referenced from Category and Suppliers tables. There are more then one options to do this but I use below way to select from multiple tables and bind to grid. First of all create a class and create properties, ProductID, ProductName, CompanyName, CategoryName, UnitPrice. These are the properties which I want to show in grid. public class AvailableProducts { public int ProductID { get ; set ; } public string ProductName { get ; set ; } public string CompanyName { get ; set ; } public string CategoryName { get ; set ; } public decimal UnitPrice { get ; set ; } } Now, write below function in data access layer: public List < AvailableProducts > GetAllAvailableProducts() { var linqAllProducts = from p in NorthwindEntity.Products from c in NorthwindEntity.Categories from s in NorthwindEntity.Supp

LINQ Bulk Insert Update Delete

After long time I have free time so I think this is the best time to update my blog with new post and this time I choose to write something on bulk database operations. Well, when I came across the requirement of bulk database operations in one of my project, I search hard on net and finally I found one method that I can use.  I am going to show the method which I use in one of my project for bulk insert, update and delete. During working on bulk database operations from LINQ to entities, first thing came in my mind is transaction scope. i.e.I should have command on each of the database transactions I am going to do from my data access layer so that in case of error I can easily rollback all the database changes. To archive transaction scope functionality, I use “System.Transactions.TransactionScope" class. Below is the code I use for bulk insert. public void BulkInsert( Order [] orders) { try { using (System.Transactions. TransactionScope scop = new System.Transactions. Transac

LINQ To DataTable

Sorry for not choosing bulk insert / update again in this post. I select this topic LINQ to DataTable because I think this is a very interesting topic to post. In one of the project  in my company, I saw many DataTable operations using the old style – dt.Select() method to filter DataRows. There are lots of chances of errors at run time because all conditions are simple strings written in “.Select()” method. If you have LINQ support in your project then LINQ to DataTable is the best way to filtering datarows from DataTable. This will also reduce run time errors and I also found that LINQ takes less time to execute then Select() method if amount of rows in DataTable is more. So, let’s start by creating one temporary DataTable. We will use this DataTable to understand LINQ to DataTable. Create new DataTable // Create new DataTable DataTable dtOrders = new DataTable (); // Add columns you need dtOrders.Columns.Add( "OrderID" , typeof ( int )); dtOrders.Columns.Add( "O

Lambda Expressions In Entity Framework

From the two options I mentioned in my last post, I chose lambda expression for this post for those programmers who are not much comfortable with SQL like LINQ query. Microsoft gives us very good option called “Lambda Expression”. In this post I am going to demonstrate below operations: Simple select query using LINQ & Simple select query using lambda expression Select query with where condition using LINQ & select query with where condition using lambda expression Inner Join using LINQ & Inner Join using lambda expression Left Join using LINQ & Left Join using lambda expression Group by using LINQ & Group by using lambda expression Sorting using LINQ & Sorting using lambda expression Simple select: // Using LINQ Employee [] allEmployees1 = ( from E in NorthwindEntity.Employees select E).ToArray(); // Using lambda expression Employee [] allEmployees2 = NorthwindEntity.Employees.ToArray(); // Where condition using LINQ Employee [] allEmployees1 = ( from

LINQ To Entity Framework Tutorial – Part II

In this post, I am going to explain below operations in LINQ. Sorting Group By Left Join 1) Sorting: Ascending: Employee [] AllEmployees = ( from E in NorthwindEntity.Employees orderby E.FirstName ascending select E).ToArray(); Descending: Employee [] AllEmployees = ( from E in NorthwindEntity.Employees orderby E.FirstName descending select E).ToArray(); 2) Group By: var AllEmployee = from E in NorthwindEntity.Employees group E by E.LastName into G select new { LastName = G.Key, Group = G }; foreach ( var employees in AllEmployee) { // employees.Group is the current group // Here you will find each group ( using employees.Group ) for employees.LastName foreach ( var employee in employees.Group) { // Here you will find each employee in particular group // You can access each property of Employee object here. } } 3) Left Join: var allProductsWithCategory = from p in NorthwindEntity.Products let c = ( from c in NorthwindEntity.Categories where c.CategoryID == p.CategoryI

LINQ To Entity Framework Tutorial

As a startup, I choose this topic for those beginners who wants to use Entity Framework. The ADO.NET Entity Framework helps developers to access database very easily. All you need is to create a data model from actual database and programming against generated data model instead of programming directly against the actual database by either creating stored procedure or writing select/insert/update/delete query in data access layer itself. Using this way of programming during the development phase of the project helps to reduce amount of code to access database and also reduce maintenance work in future. Please follow below steps in order to create data access application using ADO.NET Entity Framework. In this tutorial I am using “Northwind” database in backend. Create new “ASP.Net Empty Web Application” using Visual Studio 2010. Create new folder under web application project. Name it “DataAccess” Right click on “DataAccess” folder and click on Add –> New Item… Select “Dat