Linq to Sql Query Debugger provided by Scott Guthrie provides you with one of the many options to show the actual sql that will get generated by the Linq to Sql Engine.
Additionally You can view the query, execute it and see the results - all within a debugger visualizer.It is quite a handy tool to look under the skin and understand how Linq to Sql works.
I will show you how to configure and use it in a few quick steps.You should need only a few minutes to start appreciating this important tool.
I am using the same code as the one posted here so that you dont have to reinvent the wheel for this session.
Download the full document and step by step instructions on this topic over here .
Thursday, February 25, 2010
Monday, February 15, 2010
Reading and Creating Xml Documents using Linq to XML
Xml has become sort of a de-facto standard for data transfer between systems due to its extremely
verbose layout and a host of parsers providing developers with ways to to play around with data in the Xml files.
Having said that some of the xml document nodes can run into multiple levels
of hierarchy and generating them using code can be tedious and annoying for the first time.
If you have been using XML DOM for creating these documents,then you would understand the pain
creating the XmlElements , managing the parent child links and finally creating the XmlDocument.
I would suggest that you have a look at using Linq to Xml for this purpose where the code is lot more
cleaner and if not 100% , you still are provided with some type of visual understanding of where you are
heading to.Life is a lot more easier this way.
Imagine yourself having to create the document below using Xml Dom Syntax.
We did not have may choices before and hence this approach was good but with Linq you will find that
such document creation is quite easy.
In this sample I am reading/creating Xml documents using Xml DOM as well as Linq.
The code you feel is easy to manage/understand is entirely for you to decide.
Just focus on 2 functions in the code CreateXMLDOM() and CreateXMLLinq().
The whole source and support files are included in the zip
verbose layout and a host of parsers providing developers with ways to to play around with data in the Xml files.
Having said that some of the xml document nodes can run into multiple levels
of hierarchy and generating them using code can be tedious and annoying for the first time.
If you have been using XML DOM for creating these documents,then you would understand the pain
creating the XmlElements , managing the parent child links and finally creating the XmlDocument.
I would suggest that you have a look at using Linq to Xml for this purpose where the code is lot more
cleaner and if not 100% , you still are provided with some type of visual understanding of where you are
heading to.Life is a lot more easier this way.
Imagine yourself having to create the document below using Xml Dom Syntax.
We did not have may choices before and hence this approach was good but with Linq you will find that
such document creation is quite easy.In this sample I am reading/creating Xml documents using Xml DOM as well as Linq.
The code you feel is easy to manage/understand is entirely for you to decide.
Just focus on 2 functions in the code CreateXMLDOM() and CreateXMLLinq().
The whole source and support files are included in the zip
Sunday, February 14, 2010
Lambda Expressions(c#) explained
The core to writing terse LINQ queries and expression is understanding how LAMBDA Expressions work.
Basically these are Anonymous delegates crunched upto one line of code.
The fully documented code is here .
Remember to keep the fully documented code open/visible to understand the explaination below.
A brief primer on Lambda expressions follows:
Lambda expressions are available with .NET 3.5.
Apart from making the code more readable we can mix it with other .NET features
like generics and anonymous methods.
Lambda expressions are confusing at the first look but understanding them is the code to
writing real chiq LINQ Code, be it LINQ to SQL or LINQ to XML.
What is a Lambda Expression?
In simple words it is a shortcut to Anonymous methods/delegates.
Anonymous Methods and Standard methods?
Standard methods are defined in code, given a unique name with predefined parameter and
later called from somewhere else.
Anonymous functions are defined inplace and the easiest method to identify a Lambda expression
is the presence of '=>' operator.
To the left of the => will be the parameter list and on the right side will be the
expression to be evaluated.
Lambda expressions define a function's parameters and its body.
It has the limitation that the body must be a single expression which returns a single object.
They can be used anywhere a delegate would be used. They are callback methods.
Let me show you an example of a call back with a delegate
1.Define a delegate signature
public delegate int CallBackFunction(int x, int y);
2.Declare function matching signature as delegate
static int MultiplyNumbers(int x, int y)
{
return x * y;
}
3. Any code that calls this method must pass a callback of the callBack delegate type.
protected int CalculateProduct(IEnumerable values,CallBackFunction callBack)
{
int result = 0;
foreach (int value in values)
result callback(result, value);
return result;
}
The same code can be called using Anonymous Function without using a reference to MultiplyNumbers() as
product = CalculateProduct(numbers, delegate(int x, int y)
{
return x * y;
}
);
This tells the compiler that the parameter list and the method body follows.
ie. the inline function delegate(int x, int y)
{
return x * y;
}
delegate (int x, int y) is the parameter list which in Lambda equals (int x,int y)
Hence in Lambda expression (int x,int y) => which will for the left hand side of the expression.
The right hand side of the expression ie. the predicate will form the result ie x * y
Hence the function
product = CalculateProduct(numbers, delegate(int x, int y)
{
return x * y;
}
);
when written in lambda will be
product = CalculateProduct(numbers, (x,y)=>x*y);
I would really suggest you to try out a few examples and get used to the Lambda syntax as it
is really a very cool feature and you just cannot miss having it in your programming skill armoury.
Basically these are Anonymous delegates crunched upto one line of code.
The fully documented code is here .
Remember to keep the fully documented code open/visible to understand the explaination below.
A brief primer on Lambda expressions follows:
Lambda expressions are available with .NET 3.5.
Apart from making the code more readable we can mix it with other .NET features
like generics and anonymous methods.
Lambda expressions are confusing at the first look but understanding them is the code to
writing real chiq LINQ Code, be it LINQ to SQL or LINQ to XML.
What is a Lambda Expression?
In simple words it is a shortcut to Anonymous methods/delegates.
Anonymous Methods and Standard methods?
Standard methods are defined in code, given a unique name with predefined parameter and
later called from somewhere else.
Anonymous functions are defined inplace and the easiest method to identify a Lambda expression
is the presence of '=>' operator.
To the left of the => will be the parameter list and on the right side will be the
expression to be evaluated.
Lambda expressions define a function's parameters and its body.
It has the limitation that the body must be a single expression which returns a single object.
They can be used anywhere a delegate would be used. They are callback methods.
Let me show you an example of a call back with a delegate
1.Define a delegate signature
public delegate int CallBackFunction(int x, int y);
2.Declare function matching signature as delegate
static int MultiplyNumbers(int x, int y)
{
return x * y;
}
3. Any code that calls this method must pass a callback of the callBack delegate type.
protected int CalculateProduct(IEnumerable
{
int result = 0;
foreach (int value in values)
result callback(result, value);
return result;
}
The same code can be called using Anonymous Function without using a reference to MultiplyNumbers() as
product = CalculateProduct(numbers, delegate(int x, int y)
{
return x * y;
}
);
This tells the compiler that the parameter list and the method body follows.
ie. the inline function delegate(int x, int y)
{
return x * y;
}
delegate (int x, int y) is the parameter list which in Lambda equals (int x,int y)
Hence in Lambda expression (int x,int y) => which will for the left hand side of the expression.
The right hand side of the expression ie. the predicate will form the result ie x * y
Hence the function
product = CalculateProduct(numbers, delegate(int x, int y)
{
return x * y;
}
);
when written in lambda will be
product = CalculateProduct(numbers, (x,y)=>x*y);
I would really suggest you to try out a few examples and get used to the Lambda syntax as it
is really a very cool feature and you just cannot miss having it in your programming skill armoury.
A test drive with Linq
This is my first post on Linq.
Having worked on Linq for some time I have wanted to start writing something
on this area but other areas or interest took over and I could not focus on writing on LINQ.
The first post/example will introduce you to writing Linq syntax as well as a comparision of
LINQ style of coding to the more Traditional approach that most of us are used to writing our code.
I am sure you will appeciate the features that LINQ provides.
Get use to the syntax and understand the power of LINQ and you will find out how much less code
you have to write to achieve the same result as with some of the approaches we have been using.
Most of the comparitive study and comments are part of the code inline and very easy to understand.
The code for this demo can be downloaded here.
Having worked on Linq for some time I have wanted to start writing something
on this area but other areas or interest took over and I could not focus on writing on LINQ.
The first post/example will introduce you to writing Linq syntax as well as a comparision of
LINQ style of coding to the more Traditional approach that most of us are used to writing our code.
I am sure you will appeciate the features that LINQ provides.
Get use to the syntax and understand the power of LINQ and you will find out how much less code
you have to write to achieve the same result as with some of the approaches we have been using.
Most of the comparitive study and comments are part of the code inline and very easy to understand.
The code for this demo can be downloaded here.
Subscribe to:
Posts (Atom)