http://asp-net-example.blogspot.in/search/label/Linq%20Example















Linq Average Operator - How to get the average of a sequence of numeric values
LinqAverageOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- int[] prices = {21,23,29,18,33,27,29};
- double averagePrice = prices.Average();
- Label1.Text = "Price List: <br />";
- foreach (int n in prices)
- {
- Label1.Text += n.ToString() + "<br />";
- }
- Label2.Text = "Average Price: " + averagePrice.ToString() +"<br />";
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Average Operator - How to get the average of a sequence of numeric values</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator - Average
- <br />How to get the average of a
- <br /> sequence of numeric values
- </h2>
- <hr width="375" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="Large"
- ForeColor="Crimson"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="Large"
- ForeColor="DodgerBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Average"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq Union Operator - How to get a sequence representing the union of two sequences
Linq Union Operator - How to get a sequence representing the union of two sequences
LinqUnionOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- int[] nums1 = { 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10 };
- int[] nums2 = { 1, 3, 3, 5, 7, 9, 11 };
- Label1.Text = "Number List 1:....... <br />";
- foreach (int n in nums1)
- {
- Label1.Text += n.ToString() + ", ";
- }
- Label1.Text += "<br /><br />Number List 2:....... <br />";
- foreach (int n in nums2)
- {
- Label1.Text += n.ToString() + ", ";
- }
- var val = nums1.Union(nums2);
- Label2.Text = "<br />After Call Union Operator [nums1.Union(nums2)]:....... <br />";
- foreach (int n in val)
- {
- Label2.Text += n.ToString() + ", ";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Union Operator - How to get a sequence representing the union of two sequences</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Set Operator - Union
- <br />How to get a sequence representing the union of two sequences
- </h2>
- <hr width="625" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="SeaGreen"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkMagenta"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Union"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq ToList Operator - How to get a List from a sequence
Linq ToList Operator - How to get a List from a sequence
LinqToListOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- string[] fruits = { "Kokosboom", "Korean mango", "Litchi chinensis", "Mafai", "Knoeper", "Mangostin" };
- Label1.Text = "Fruits List:....... <br />";
- foreach (string item in fruits)
- {
- Label1.Text += item.ToString() + "<br />";
- }
- List<string> query = (from f in fruits
- where f.StartsWith("K")
- orderby f
- select f).ToList();
- Label2.Text = "Query Result Type: " + query.GetType() + "<br /><br />";
- Label2.Text += "Query Result:.... <br />";
- for (int i = 0; i < query.Count; i++)
- {
- Label2.Text += query[i].ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq ToList Operator - How to get a List from a sequence</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Conversion Operator - ToList
- <br />How to get a List from a sequence
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MidnightBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="Crimson"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - ToList"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq ToArray Operator - How to get an Array from a sequence
Linq ToArray Operator - How to get an Array from a sequence
LinqToArrayOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- string[] fruits = { "Flor de Jamaica", "Fraise", "Fruit du dragon", "Grape", "Gulupa", "Kathon" };
- Label1.Text = "Fruits List:....... <br />";
- foreach (string item in fruits)
- {
- Label1.Text += item.ToString() + "<br />";
- }
- string[] query = (from f in fruits
- where f.StartsWith("F")
- orderby f
- select f).ToArray();
- Label2.Text = "Query Result: Items Count - " + query.Length.ToString() + "<br />";
- for (int i = 0; i < query.Length; i++)
- {
- Label2.Text += query[i].ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq ToArray Operator - How to get an Array from a sequence</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Conversion Operator - ToArray
- <br />How to get an Array from a sequence
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MidnightBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkMagenta"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - ToArray"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq OfType Operator - How to filter elements in a sequence of a given type
Linq OfType Operator - How to filter elements in a sequence of a given type
LinqOfTypeOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- ArrayList items = new ArrayList();
- items.Add(1);
- items.Add("Crataeva marmelos");
- items.Add(2);
- items.Add("Durian Belanda");
- items.Add(3);
- items.Add("Figuier commun");
- Label1.Text = "ArrayList (items):....... <br />";
- foreach (var item in items)
- {
- Label1.Text += item.ToString() + "<br />";
- }
- IEnumerable<int> query = items.OfType<int>().Select(i => i);
- Label2.Text = "Query Result After Call OfType Operator:....... <br />";
- foreach (int item in query)
- {
- Label2.Text += item.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq OfType Operator - How to filter elements in a sequence of a given type</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Conversion Operator - OfType
- <br />How to filter elements in a sequence of a given type
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkGreen"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="Purple"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - OfType"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq Intersect Operator - How to get a sequence representing the intersection of two sequences
Linq Intersect Operator - How to get a sequence representing the intersection of two sequences
LinqIntersectOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- int[] nums1 = { 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10 };
- int[] nums2 = { 1, 3, 3, 5, 7, 9, 9, 11 };
- Label1.Text = "Number List 1 (nums1):....... <br />";
- foreach (int n in nums1)
- {
- Label1.Text += n.ToString() + ", ";
- }
- Label1.Text += "<br /><br />Number List 2 (nums2):....... <br />";
- foreach (int n in nums2)
- {
- Label1.Text += n.ToString() + ", ";
- }
- var val = nums1.Intersect(nums2);
- Label2.Text = "<br />After Call Intersect Operator [nums1.Intersect(nums2)]:....... <br />";
- foreach (int n in val)
- {
- Label2.Text += n.ToString() + ", ";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Intersect Operator - How to get a sequence representing the intersection of two sequences</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Set Operator - Intersect
- <br />How to get a sequence representing the intersection of two sequences
- </h2>
- <hr width="675" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MediumOrchid"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="Purple"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Intersect"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq Except Operator - How to get a sequence representing the difference between two sequences
Linq Except Operator - How to get a sequence representing the difference between two sequences
LinqExceptOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- int[] nums1 = { 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10 };
- int[] nums2 = { 1, 3, 3, 5, 7, 9, 9, 11, 13, 13 };
- Label1.Text = "Number List 1 (nums1):....... <br />";
- foreach (int n in nums1)
- {
- Label1.Text += n.ToString() + ", ";
- }
- Label1.Text += "<br /><br />Number List 2 (nums2):....... <br />";
- foreach (int n in nums2)
- {
- Label1.Text += n.ToString() + ", ";
- }
- var val = nums1.Except(nums2);
- Label2.Text = "<br />After Call Except Operator [nums1.Except(nums2)]:....... <br />";
- foreach (int n in val)
- {
- Label2.Text += n.ToString() + ", ";
- }
- var val2 = nums2.Except(nums1);
- Label2.Text += "<br /><br />After Call Except Operator [nums2.Except(nums1)]:....... <br />";
- foreach (int n in val2)
- {
- Label2.Text += n.ToString() + ", ";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Except Operator - How to get a sequence representing the difference between two sequences</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Set Operator - Except
- <br />How to get a sequence representing the difference between two sequences
- </h2>
- <hr width="675" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MediumSeaGreen"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="Purple"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Except"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


Linq Cast Operator - How to cast elements in a sequence to a given type
Linq Cast Operator - How to cast elements in a sequence to a given type
LinqCastOperator.aspx
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <%@ Import Namespace="System.Linq" %>
- <!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 Button1_Click(object sender, System.EventArgs e)
- {
- ArrayList fruits = new ArrayList();
- fruits.Add("Afrikanische Malve");
- fruits.Add("Aprikosenbaum");
- fruits.Add("Averrhoa carambola");
- fruits.Add("Cerasus avium");
- fruits.Add("Date palm");
- Label1.Text = "ArrayList (fruits):....... <br />";
- foreach (string item in fruits)
- {
- Label1.Text += item.ToString() + "<br />";
- }
- IEnumerable<string> query = fruits.Cast<string>().Select(f => f);
- Label2.Text = "Query Result:....... <br />";
- foreach (string item in query)
- {
- Label2.Text += item.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Cast Operator - How to cast elements in a sequence to a given type</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Conversion Operator - Cast
- <br />How to cast elements in a sequence to a given type
- </h2>
- <hr width="575" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MediumSeaGreen"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="Purple"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Cast"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


No comments:
Post a Comment