Contents tagged with Linq
Deferred vs Immediate execution in Linq
In this post, We are going to learn about Deferred vs Immediate execution in Linq. There an interesting variations how Linq operators executes and in this post we are going to learn both Deferred execution and immediate execution.
What is Deferred Execution?
In the Deferred execution query will be executed and evaluated at the time of query variables usage. Let’s take an example to understand Deferred Execution better.
Example:
Following is a code for that.
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var customers = new List<Customer>( new[] { new Customer{FirstName = "Jalpesh",LastName = "Vadgama"}, new Customer{FirstName = "Vishal",LastName = "Vadgama"}, new Customer{FirstName = "Tushar",LastName = "Maru"} } ); var newCustomers = customers.Where(c => c.LastName == "Vadgama"); customers.Add(new Customer {FirstName = "Teerth", LastName = "Vadgama"}); foreach (var c in newCustomers) { Console.WriteLine(c.FirstName); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } } } }
More on my personal blog @
SelectMany operator in Linq C#
Select; } }
Where I can find SQL generated by Linq-To-SQL
Yesterday I have written a blog post about Where I can find SQL Generated by Entity Framework? and same day I got request from one of the our reader Ramesh that how I can find SQL generated by Linq-To-SQL?. I thought its a good idea to write a blog post to share amongst all who need this instead of reply in comments. In this post I am going to explain how we can get SQL generated by Linq-To-SQL.
For this post I am going to use same table like following. A customer table with two columns CustomerId and CustomerName.
Now we have already have table to its time to create a Linq-To-SQL dbml file..
Where operator filters a list of collection based on condition. It will filter whole collection while SkipWhile will only skip those elements in list until condition is true and after that it will stop filtering of skipping. Now let’s take an example where we can see the difference between Where and SkipWhile. Following is a code for that.
using System; using System.Collections.Generic; using System.Linq; namespace Linq { class Program { static void Main(string[] args) { string[] names = { "Jalpesh", "Jayesh", "Tushar", "Tejas", "Sanjay", "Nijesh","Jemit","Jay" }; Console.WriteLine("Skipwhile Operator"); foreach (var name in names.SkipWhile(s => s.ToLower().StartsWith("j"))) { Console.WriteLine(name); } Console.WriteLine("-----------------------------------"); Console.WriteLine("Where Operator"); foreach (var name in names.Where(s => !s.ToLower().StartsWith("j"))) { Console.WriteLine(name); } } } }
As you can see that in the above code I have a string array called names where I have people’s name as string. Here you can see there are multiple names start with letter “J” in array. Now once we use SkipWhile operator it will skip the elements untill condition is false for first time So it will skip the elements until condition satisfied for the first once you have different condition it will not going skip element even if they are satisfying the condition. While in where it will filter whole collection and print output.
Let’s run above example. Following is a output as expected.
That’s it. Hope you like it. Stay tuned for more. Till than happy programming.
SkipWhile Method in Linq
I have been playing around linq and I have found great method call SkipWhile method. SkipWhile methods skips particular element which matches condition in predicate this can be use full in condition where we need to skip elements on particular condition.
So let’s take some example. I have written following console application code.
using System; using System.Collections.Generic; using System.Linq; namespace Linq { class Program { static void Main(string[] args) { string[] names = { "Jalpesh", "Jayesh", "Tushar", "Tejas", "Sanjay", "Nijesh" }; foreach(var name in names.SkipWhile(s=>s.ToLower().StartsWith("j"))) { Console.WriteLine(name); } } } }
In the above code I have created a array of string called names and I want to skip names starts with letter “j” with help of ‘SkipWhile’. I am printing each item via for each loop. Following is the output as expected.
That’s it.Hope you liked it. Stay tuned for more..Till then happy programming.
Creating Basic RSS Reader in ASP.NET MVC 3
In this post I am going to explain you how we can create a basic RSS Reader with the help of Linq-To-Xml and ASP.NET MVC3 Razor. Those who are writing or reading Blogs already knows what is RSS Reader. But those who does not know What is RSS. Below is the definition for RSS as per Wikipedia..
You can find more information about RSS from the following links..
Now let’s start writing code creating a Basic RSS Reader. So first We need two things to create RSS Reader. A RSS Entity class which hold properties for RSS and Another method which populate IEnumerable of particular RSS Class. We are creating this example with ASP.NET So I have create One Model class called RSS Like following.
using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text.RegularExpressions; namespace CodeSimplified.Models { public class Rss { public string Link { get; set; } public string Title { get; set; } public string Description { get; set; } } }
Now our entity class is ready. Now we need a class and a method which will return IEnumerable of RSS Class. So I have created a Static Class RSS Reader which has “GetRSSFeed” Method which return RSS Feeds like following.
using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text.RegularExpressions; namespace CodeSimplified.Models { public class RssReader { private static string _blogURL = ""; public static IEnumerable<Rss> GetRssFeed() { XDocument feedXml = XDocument.Load(_blogURL); var feeds = from feed in feedXml.Descendants("item") select new Rss { Title = feed.Element("title").Value, Link = feed.Element("link").Value, Description = Regex.Match(feed.Element("description").Value, @"^.{1,180}\b(?<!\s)").Value }; return feeds; } } }
As you can see in above code. I am loading RSS feed with XDcoument Class with my Blog RSS feed URL and Then I am populating RSS Class Enumerable with the help of the Linq-To-XML. Now We are ready with Our Model classes so Now it’s time to Add ActionResult in Home Controller. So I have added Action Result which return View with RSS IEnumerable like following.
public ActionResult RssReader() { return View(CodeSimplified.Models.RssReader.GetRssFeed()); }
Now everything is ready. So its time to create a view. So I have created strongly typed view for RSS Model class like following.
@model IEnumerable<CodeSimplified.Models.Rss> @{ ViewBag.@Html.Encode(item.Title)</a> </td> <td> @System.Web.HttpUtility.HtmlDecode(item.Description) </td> <td> <a href="@item.Link" target="_blank">More</a> </td> </tr> } </table>
Let's run application via pressing F5 and Following is the output as expected.
So that’s it. Isn’t that cool? With the few lines of code we have created a Basic RSS Reader. Hope you like it. Stay tuned for more.. Till then Happy Programming..
Distinct operator in Linq
Linq operator provides great flexibility and easy way of coding. Let’s again take one more example of distinct operator. As name suggest it will find the distinct elements from IEnumerable. Let’s take an example of array in console application and then we will again print array to see is it working or not. Below is the code for that. In this application I have integer array which contains duplicate elements and then I will apply distinct operator to this and then I will print again result of distinct operators to actually see whether its working or not.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiment { class Program { static void Main(string[] args) { int[] intArray = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 }; var uniqueIntegers = intArray.Distinct(); foreach (var uInteger in uniqueIntegers) { Console.WriteLine(uInteger); } Console.ReadKey(); } } }
Below is output as expected..
That’s cool..Stay tuned for more.. Happy programming.
GUID Vs Int data type as primary key
Recently comment as your opinion.Technorati Tags: GUID,INT,SQL Server2008,SQL Server
LinqDatasource A Great Control for declarative programming
I
<>
Then go to Linq Data source and set EnableDelete and EnableUpdate property to true as we need this functionality. Just like following.
<asp:LinqDataSource
</asp:LinqDataSource>
That’s it now everything is ready lets run the example and see how its works here is the update example and its working fine as should.
So It’s very easy to create this kind of functionality. Hope this help you.. Happy Programming..Technorati Tags: Linq-To-Sql,LinqDataSource,Linq,ASP.NET,Datasource
Using Let Keyword in Linq
I!!!
Technorati Tags: Let,Linq-To-Sql,Linq