Posts

Showing posts with the label LINQ

Create xml from database using LINQ

Image
Create XML from Database Suppose we have below table in database. Now I want to make xml of all the products which have stock greater than 0. Here I am using  LINQ Pad  to query data from the database. It is a great tool to query data from database using LINQ to SQL, LINQ to XML, LINQ to Entity Framework. CREATE TABLE Product ( ProductID int IDENTITY ( 1 , 1 ) NOT NULL , ProductName varchar ( 50 ) NOT NULL , Price float NOT NULL , Stock int NOT NULL ) GO INSERT INTO Product ( ProductName , Price , Stock ) VALUES ( 'P001' , 12.12 , 100 ) INSERT INTO Product ( ProductName , Price , Stock ) VALUES ( 'P002' , 102.12 , 200 ) INSERT INTO Product ( ProductName , Price , Stock ) VALUES ( 'P003' , 104.12 , 500 ) INSERT INTO Product ( ProductName , Price , Stock ) VALUES ( 'P004' , 108.12 , 100 ) INSERT INTO Product ( ProductName , Price , Stock ) VALUES ( 'P005' , 72.12 , 10 ) INSERT INTO Product ( ProductName , ...

"IN" operator in LINQ

SQL Query using IN operator SELECT Members . Name FROM Members WHERE Members . ID IN ("1,2,3" ) LINQ Query equivalent string [] str = { "1" , "2" }; var list = Members .Where(p=> str.Contains(p.ID)) .Select(X => X);

LINQ To SQL - CASE Statements

Image
Switch functionality can be accomplished by using " Tern ary Operator " The ternary operator takes the "if" statement and turns it into an expression.  Here's an example: The syntax is <condition>  ? <true value> : <false value> Now, let's add a basic case statement.  This will return the text "This is poisonous!" for plants with a 0 in the edible field, and "Okay to eat" otherwise.  By looking at the generated SQL using the debug visualizer, we can see that a CASE statement is in fact being generated. Now the question will come up, "what if I want to have more than just one WHEN and an ELSE".  In other words, how do I add more cases.  Here's the trick: By replacing the "if false" value of the ternary expression with another ternary expression we logically create the same effect as a SQL CASE statement.  Unlike the switch statement, this is an expression, and can be used on the ...

Types of LINQ syntax

There are 2 types of LINQ Syntax: 1.Fluent syntax 2.Query syntax string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" }; IEnumerable<string> query = names.Where (name => name.EndsWith ("y")); query.Dump ("In fluent syntax"); query = from n in names where n.EndsWith ("y") select n; query.Dump ("In query syntax");

Combining interpreted and local queries.

void Main() { // This uses a custom 'Pair' extension method, defined below. IEnumerable<string> q = Customers .Select (c => c.Name.ToUpper()) .Pair() // Local from this point on. .OrderBy (n => n); q.Dump(); } public static class MyExtensions { public static IEnumerable<string> Pair (this IEnumerable<string> source) { string firstHalf = null; foreach (string element in source) if (firstHalf == null) firstHalf = element; else { yield return firstHalf + ", " + element; firstHalf = null; } } }

SQL to LINQ converter

Try this tool :  http://www.linqpad.net/

Using LINQPad to test your LINQ to XML queries

Image
Using LINQPad to test your LINQ to XML queries Sunday, 6 February 2011 12:55 by  mha If you’re a LINQPad user you’re probably used to query your DBML (LINQ to SQL), but did you know that you can also use it for LINQ to Objects, LINQ to XML etc. A simple example of how to do a query against a XML file (make sure language is set to C# Statements): var xml = XElement.Load (@"c:\\inetpub\\GWportal\\src\\wwwBackend\\App_Data\\axCache\\     GWDK\productprice-mha.xml"); var query =    from e in xml.Descendants("unittransaction").Descendants()      where (e.Attribute("name").Value == "ItemRelation" && e.Value.Equals("10020"))    select e.Parent;     query.Dump(); Which gives this result: