Posts

Showing posts with the label LINQ

Advantages and Disadvantages of LINQ

Advantages Quick turn around for development It is a cleaner and typesafety. Queries can be dynamically Tables are automatically created into class Columns are automatically created into properties Relationship are automatically appeaded to classes Lambda expressions are awesome Data is easy to setup and use It is checked by the compiler instead of at runtime It can be debugged easily. It can be used against any datatype - it isn't limited to relational databases, you can also use it against XML, regular objects. It can able to query not just tables in a relational database but also text files and XML files. Disadvantages No clear outline for Tiers No good way of view permissions Small data sets will take longer to build the query than execute There is an overhead for creating queries When queries are moved from sql to application side, joins are very slow DBML

How to use Join with LINQ

using System; using System.Linq; using System.Configuration; public partial class LINQWithJoin : System.Web.UI. Page {     TestDataClassesDataContext objDBContext = new TestDataClassesDataContext ( ConfigurationManager .ConnectionStrings[ "TestConnectionString" ].ToString());     protected void Page_Load( object sender, EventArgs e)     {     }     protected void btnDisplay_Click( object sender, EventArgs e)     {         var Allcity = from city in objDBContext.City_Tbls join state in objDBContext.State_Tbls on city.State_Id equals state.State_Id                              select new                             {                                CityName=city.Name,                                StateName=state.Name                             };         GridView1.DataSource = Allcity;         GridView1.DataBind();         } } int above code CityName & StateName works as a alias from city.Name

How to use SQL Query with LINQ

Add LINQ to SQL Class from Add New Item Template and name it TestDataClassesDataContext Add Database table in to TestDataClassesDataContext Create a   TestDataClassesDataContext object into default.aspx.cs   page   TestDataClassesDataContext objDBContext = new TestDataClassesDataContext ( ConfigurationManager .ConnectionStrings[ "TestConnectionString" ].ToString());   //Bind Gridview using LINQ with Query // User_Tbls is User_Table(DataBase table) class var userdetails = from user in objDBContext.User_Tbls select user;         GridView1.DataSource = userdetails;         GridView1.DataBind();

Stored Procedure with LINQ for Insert

Image
How to Use Stored Procedure in LINQ for Insert 1. Crate the table for Example "User_Tbl" such as 2. Create the a Web Application       Right click on project ->Add New Item-> select LINQ to SQL Class and give the name to TestDataClasses click on OK under the App_Code Create a file TestDataClasses.dbml with extension .dbml double click on it. 3. Connect To Database  in Visual Studio for this go to Tool->Connect To DataBase after connecting you can see all the database objects in Server Explorer. Drag and Drop the required objects (such as Table, Stored Procedure etc. on TestDataClasses.dbml. Save the TestDataClasses.dbml now   you can get the DB objects in your page the TestDataClasses show  such as  Example: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; public partial class LinqStoredProcedur