Wriju's BLOG

.NET, Cloud and everything

LINQ to SQL : Windows Forms Dynamic Where Clause

While I was reading the book LINQ in Action, I found this chapter. Below is how we can do in LINQ to SQL. The scenario is if you want to create a LINQ and pass the where clause based on user input from UI, you need to write different queries. Instead, how you can do… Read more

LINQ – Uniformity All Over

I have spent a while with LINQ and still feel very new whenever I explore some new power. Here I am going to describe you how LINQ uniformly allows you write for the various types of data.   In-memory data source ++++++++++++++++ List<int> arrInt = new List<int> { 1, 2, 3, 4, 5, 6, 7,… Read more

LINQ to SQL : Understanding Compiled Query

Understanding Compiled Query connects me to my experience in C# 3.0. Based on my personal understanding I am discussing it.   You may be aware of Func<>. And you know it is a flexible delegate which allows you create reusable functions. Exactly the same way we can create compiled query so that we can prevent… Read more

LINQ to SQL : Returning Scalar Value from Stored Procedure

In LINQ to SQL it is not that easy thing to achieve as compared to other features. Let us assume you have a Stored Procedure like, Case 1: With Output Parameter CREATE PROCEDURE [dbo].[GetEmployeeCount]       @OutVal DateTime OUTPUT AS BEGIN       SELECT @OutVal = GetDate() END You need to write code which will look like,… Read more

LINQ to SQL : Using ChangeSet for Audit Trail

Most of the transactional scenario, you may need to add the transaction to some table for ref. many apps does it for admin perspective.   Problem   You can do it in LINQ to SQL with ChangeSet but the problem is with Inserts. It does not give you the Identity field value until and unless… Read more

Choosing between ADO.NET Entity Framework and LINQ to SQL

As part of my day to job I come across a very common question from the developer community that one should be using LINQ to SQL (L2S) or ADO.NET Entity Framework (EF).   Earlier I have posted a blog containing the features http://blogs.msdn.com/wriju/archive/2009/01/05/choosing-between-linq-to-sql-and-entity-framework.aspx.    To me I feel very excited about EF as it has got… Read more

LINQ to SQL vs. ADO.NET – A Comparison

ADO.NET is our contemporary data access component and now we have written many applications. Now there has been a lot of talk on LINQ to SQL. So we are little skeptical about this component and trying to find some relation between existing ADO.NET technology.   Purpose of this post is to give some quick brief… Read more

LINQ to SQL : Missing Manual are at MSDN

ADO.NET and LINQ to SQL Describes the relationship of ADO.NET and LINQ to SQL.   Analyzing LINQ to SQL Source Code Describes how to analyze LINQ to SQL mapping by generating and viewing source code from the Northwind sample database.   Customizing Insert, Update, and Delete Operations (LINQ to SQL) Describes how to add validation… Read more

LINQ to SQL: Making it N-Tier

You can create your application use LINQ to SQL and make it N-Tier. Go ahead and watch out the MSDN Documents N-Tier and Remote Applications with LINQ to SQL LINQ to SQL N-Tier with ASP.NET LINQ to SQL N-Tier with Web Services LINQ to SQL with Tightly-Coupled Client-Server Applications Implementing Business Logic (LINQ to SQL)… Read more

LINQ : IEnumerable<T> and IQueryable<T>

IEnumerable<T> and IQueryable<T> are the two most used terms of any LINQ discussion. What I am trying to here is that I am trying to simplify the two interfaces depending on their behavior. In LINQ world we generally have few providers available within .NET Framework, like LINQ to Object, LINQ to SQL, LINQ to XML…. Read more

LINQ to SQL : FAQ Answer By Matt Warren

Wonderful composition of FAQ is available at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2769747&SiteID=1 by Matt Warren.   Q: How do I get default values from the database when inserting new entities? A: LINQ to SQL does not support using default values from the database.  The values in the entity at the time SubmitChanges is called are used instead.  You can… Read more

LINQ to SQL Overview Video

Luca, the Microsoft Lead Program Manager, he is one of my favorite presenters. The way he demonstrate uninteresting thing and make fun of himself is one everyone should learn. Please enjoy the through presentation of LINQ to SQL at http://blogs.msdn.com/lucabol/archive/2008/03/03/linq-to-sql-overview-video.aspx   Namoskar!!!… Read more

LINQ to SQL : Understanding Mapping

In LINQ to SQL you play with pure .NET objects and write your LINQ on top of that. But question might arise how this magic happens. Though we say that it is object, the question might arise in our mind that there has to be a mechanism through which it connects. Yes, there is. That… Read more

LINQ to SQL and ADO.NET

Here I am going to compare the two technologies and point some missing areas.   Let us use the Northwind database and get the list of Customers from city London. The ADO.NET approach would be,   static void Main(string[] args) {     string sConn = @”ConnectionString”;     using (SqlConnection conn = new SqlConnection(sConn))     {… Read more

LINQ to SQL : What is DataContext.Translate Method

DataContext’s Translate method converts existing IDataReader to an IEnumerable<T>. This is very useful when you may need to run raw SQL Query though System.Data.Linq. I am getting few similarities with DataContext’s ExecuteQuery.   How Translate works +++++++++++++ Assuming I am using Northwind database.     using (NWDataContext db = new NWDataContext()) {     DbConnection conn… Read more

LINQ to SQL : Enabling .dbml file for WCF

LINQ to SQL is object relational model and .dbml file generates the class file for all kind of Create, read, update and delete (CRUD) through the mapping mechanism. Ideally this should allow us to enable it for WCF Service. But for that we have to manually add DataContract and DataMember attributes. We can use .dbml’s… Read more

Visual Studio 2008 Training Kit Videos on LINQ

Visual Studio 2008 Training Kit videos are now available on web. Get some real exciting presentations   VS2008 Training Kit: What’s new in C# 3.0? VS2008 Training Kit: What’s new in Visual Basic 9.0? VS2008 Training Kit: .NET Language Integrated Query (LINQ) VS2008 Training Kit: Using LINQ with Relational Data   For more detailed list… Read more

LINQ to SQL: Working with hierarchical data

LINQ to SQL supports hierarchical data and you can easily create a query and get output from there. Let us take an example of Northwind database. Northwind has Category -> Products -> Order_Details tables. -> indicates one to many relationships here. So ideally for a Category can have multiple products and a product can have… Read more

LINQ to SQL : New method DataContext.GetCommand()

LINQ to SQL we can read the generated T_SQL query using DataContext.Log or SQLQueryVisualizer. But If you are working with WebProject the Log property not very much convenient as you have to use TextWriter to dump the generated T-SQL query.   In Orcas Beta 1 we had DataContext.GetQueryText which used to give us the generated… Read more

LINQ to SQL Windows Form Binding Data Step by Step

LINQ to SQL designer in Visual Studio allows us to create mapped class using the drag and drop feature from Server Explorer. Now that is business object which ideally you could use as your datasource and from Visual Studio 2008. So let us have this step by step, Open your Visual Studio 2008 and create… Read more

LINQ to SQL : Some of the best BLOGs

Some of the best blogs on LINQ to SQL I found are available for great learning, Scott Guthrie The Famous LINQ TO SQL Series Part 1: Using LINQ to SQL Part 2: Defining our Data Model Classes Part 3: Querying our Database Part 4: Updating our Database Part 5: Binding UI using the ASP LinqDataSource… Read more

LINQ to SQL : Change in DataContext from Beta 1 to Beta 2

During the demonstration I have noticed that two major methods of DataContext has changed from Beta 1 to Beta 2.   First one, is the new name for the class DataShape(). Now there is no class called DataShape() but we have DataLoadOptions().     var loadOption = new DataLoadOptions();            loadOption.LoadWith<Customer>(c => c.Orders);   db.LoadOptions =… Read more

LINQ to SQL : Paging Data

When we work with relational database and especially when with larger data we generally go for custom paging. This allows us to create superfast application.   The simple logic for paged query is like,   Let’s suppose you have 100 rows. Now you want to get data of page size 10 for page 2.  … Read more

LINQ to SQL : Using Transaction

LINQ to SQL uses optimistic concurrency be default. LINQ to SQL is pure in memory operation, because you normally get the data from database and store them in .NET memory and perform some pure in memory update to those objects and finally when you are done with your modification you give those data back to… Read more

LINQ and C# 3.0 Articles and Samples

LINQ: .NET Language Integrated Query By Don Box, Anders Hejlsberg LINQ to SQL: .NET Language-Integrated Query for Relational Data By Dinesh Kulkarni, Luca Bolognese, Matt Warren, Anders Hejlsberg, Kit George .NET Language-Integrated Query for XML Data By Michael Champion The .NET Standard Query Operators By Anders Hejlsberg, Mads Torgersen Overview of C# 3.0 By Anders… Read more

LINQ to SQL : Object Relational (O/R) Model

LINQ to SQL is pure Object Relational Model introduced by Microsoft. It used mapping mechanism to represent   Relational Components Object Component Table Class Column Property Foreign-key relationship Association   It gets the user input through this object model and gives the translated value to SQL in form of pure T-SQL query and when query… Read more

LINQ to SQL : Caching the query execution

LINQ to SQL executes or generated SQL statement only when you run a foreach statement or you perform to ToList() method to it. If you need to display the output multiple times without executing the real database query, you can store them in memory. This can be done only when you are sure that the… Read more

LINQ to SQL : Deferred and Immediate Loading

LINQ to SQL has support for both deferred and immediate loading. By default it supports deferred loading but you can configure your code to load the data immediately. Let’s consider the Northwind database. As we know the there are two tables Customer and Orders and one customer can have multiple orders. These two tables have… Read more

LINQ to SQL : Writing DML query to perform quick operation

In LINQ to SQL you also can send the DML query to perform the quick database modification.   Caution!!! It is not recommended as it does not support object model so has no compile time checking.   static void Main(string[] args) {     string strConnection = @”Data Source=wriju-orcas\sqlexpress;Initial Catalog=TestDB;”;       TestDB db = new… Read more

LINQ to SQL : Delete data through Object Model

LINQ to SQL support deleting data through object. Continuing with my previous blog on INSERT, let me discuss about the delete method   static void Main(string[] args) {     string strConnection = @”Connection String”;       TestDB db = new TestDB(strConnection);       db.Log = Console.Out;             //Here I am finding the employee with… Read more

LINQ to SQL : Update data through Object Model

LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT, let me discuss about the update method   static void Main(string[] args) {     string strConnection = @”Connection String”;       TestDB db = new TestDB(strConnection);         //Here I am finding the employee with Id 19     var updateQ… Read more

LINQ to SQL support inserting data through object.

LINQ to SQL support inserting data through object.   using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq;   namespace Linq2Sql_demo_doc {     /// <summary>     /// Class to represent the SQL Server table     /// Emp     /// Id int     /// Name varchar(50)     /// </summary>    ///     [Table(Name=“Emp”)]     public class… Read more

LINQ to SQL : Executing DML SQL Query

INSERT/DELETE/UPDATE can be performed in LINQ to SQL through normal T-SQL DML queries,   static void Main(string[] args) {     NorthwindDBDataContext db = new NorthwindDBDataContext();   var query = db.ExecuteCommand(“YOUR DML Query”, “Your    Parameter”);       ObjectDumper.Write(query); }   Are you feeling comfortable? Hold on, please try to avoid this as you have better object… Read more

LINQ to SQL : Executing SQL Query

Yes, you can run normal SQL queries in LINQ to SQL. Sample looks like (but not recommended as you do not have statement completion and validation)   static void Main(string[] args) {     NorthwindDBDataContext db = new NorthwindDBDataContext();       var query = db.ExecuteQuery<Customer>(“SELECT * FROM Customers”, “”);       ObjectDumper.Write(query); }   One of… Read more