The present article demonstrates – “How to use LINQ Restriction Query Operator”.
Restriction Operator –
-
Where
NOTE –
- Most of the code I have written for demonstrate purpose is self-explanatory, So intentionally I have not written whole bunch of comments explaining each and every line of the code ( as I feel that those comments make readability more cumbersome). Kindly bear with my writing style.
- There might be some problems with following the naming conventions, I regret for any inconvenience caused by it.
- The Queries are designed and implement to show various possibilities of implementations. Some queries (like finding the Product ID’s which are odd numbers etc) are may be meaningless in real world, But my request is to see the underlying implementation, means the essence of the query but not the result.
========================================================================================================
Product Class
Note – This is the class on which I use LINQ Query to get required results.
========================================================================================================
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI.WebControls; using System.Collections; public class Product { /* The four Properties of Product Class */ public string ProductName { set; get; } public int ProductId { set; get; } public string Manufacturer { set; get; } public string Drivetype { set; get; } /* Method to create a sample Product List, it returns the created Product list. */ public List<Product> CreateProducts() { List<Product> ListInitialize = new List<Product>(); ListInitialize.Add(new Product { ProductName = "Civic", ProductId = 5, Manufacturer = "Honda", Drivetype = "Auto" }); ListInitialize.Add(new Product { ProductName = "Mustang", ProductId = 11, Manufacturer = "Ford", Drivetype = "Manual" }); ListInitialize.Add(new Product { ProductName = "350z", ProductId = 2, Manufacturer = "Nissan", Drivetype = "Auto" }); ListInitialize.Add(new Product { ProductName = "Tavera", ProductId = 24, Manufacturer = "Chevrolet", Drivetype = "Auto" }); ListInitialize.Add(new Product { ProductName = "Terrain", ProductId = 1, Manufacturer = "GMC", Drivetype = "Auto" }); ListInitialize.Add(new Product { ProductName = "X6 M", ProductId = 17, Manufacturer = "BMW", Drivetype = "Manual" }); ListInitialize.Add(new Product { ProductName = "CL63 AMG Coupe", ProductId = 10, Manufacturer = "Mercedes", Drivetype = "Manual" }); return ListInitialize; } /* RepeaterBind(IEnumerable,Repeater) is used to Bind the Repeater control with the Output of * Linq Query. It takes two arguments, first one is of IEnumerable compatible (the result of Linq Query) * and second one is of Repeater Type.*/ public void RepeaterBind(IEnumerable ListPrint, Repeater repeater) { repeater.DataSource = ListPrint; repeater.DataBind(); } }
========================================================================================================
Where Operator Demo – 1
Note – This Demo uses the above mentioned Product Class, and displays the results in a Repeater.
========================================================================================================
<%@ Page Language="C#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { // Create Product Class Object Product productObject = new Product(); // Get List of Prducts List<Product> ListProduct = productObject.CreateProducts(); /* LINQ Query - This uses the Where operator of LINQ, the query gets the * list of products whose id's are greater than 10. */ var LinqQueryProducts = from product in ListProduct where product.ProductId > 10 select product; // Sends the Query and Repeater control for binding process productObject.RepeaterBind(LinqQueryProducts, Repeater1); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> Product Name :- <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label><br/> Product Id :- <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductId")%>'></asp:Label><br/> Manufacutrer :- <asp:Label ID="Label3" runat="server" Text='<%#Eval("Manufacturer")%>'></asp:Label><br/> Drivetype :- <asp:Label ID="Label4" runat="server" Text='<%#Eval("Drivetype")%>'></asp:Label><br/> </ItemTemplate> <SeparatorTemplate> <hr></hr> </SeparatorTemplate> </asp:Repeater> </div> </form> </body> </html>
OUTPUT –
========================================================================================================
Where Operator Demo – 2
Note – This Demo uses the above mentioned Product Class, and displays the results in a Repeater.
========================================================================================================
<%@ Page Language="C#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { // Create Product Class Object Product productObject = new Product(); // Get List of Prducts List<Product> ListProduct = productObject.CreateProducts(); /* LINQ Query - This uses the Where operator of LINQ, the query gets the * list of products whose ProductName statrs with 'T'. */ var LinqQueryProducts = (from product in ListProduct select product).Where((product, productBool) => product.ProductName.StartsWith("T")); // Sends the Query and Repeater control for binding process productObject.RepeaterBind(LinqQueryProducts, Repeater1); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> Product Name :- <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label><br/> Product Id :- <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductId")%>'></asp:Label><br/> Manufacutrer :- <asp:Label ID="Label3" runat="server" Text='<%#Eval("Manufacturer")%>'></asp:Label><br/> Drivetype :- <asp:Label ID="Label4" runat="server" Text='<%#Eval("Drivetype")%>'></asp:Label><br/> </ItemTemplate> <SeparatorTemplate> <hr></hr> </SeparatorTemplate> </asp:Repeater> </div> </form> </body> </html>
OUTPUT –
========================================================================================================
Where Operator Demo – 3
Note – This Demo uses the above mentioned Product Class, and displays the results in a Repeater.
========================================================================================================
<%@ Page Language="C#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { // Create Product Class Object Product productObject = new Product(); // Get List of Prducts List<Product> ListProduct = productObject.CreateProducts(); /* LINQ Query - This uses the Where operator of LINQ, the query gets the * list of products whose ProductName Length is greater than * 4 and its correpsonding Id should be greater than 10. */ var LinqQueryProducts = from product in ListProduct where product.ProductName.Length > 4 && product.ProductId > 10 select product; // Sends the Query and Repeater control for binding process productObject.RepeaterBind(LinqQueryProducts, Repeater1); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> Product Name :- <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label><br/> Product Id :- <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductId")%>'></asp:Label><br/> Manufacutrer :- <asp:Label ID="Label3" runat="server" Text='<%#Eval("Manufacturer")%>'></asp:Label><br/> Drivetype :- <asp:Label ID="Label4" runat="server" Text='<%#Eval("Drivetype")%>'></asp:Label><br/> </ItemTemplate> <SeparatorTemplate> <hr></hr> </SeparatorTemplate> </asp:Repeater> </div> </form> </body> </html>
OUTPUT –








it is good but
also explain with func delegate