Monday, August 6, 2012

Asp.net linq code samples 2


How to use Linq SingleOrDefault operator with condition
LinqSingleOrDefaultOperatorWithCondition.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Zoete sinaasappel", "Pyrus communis", "Sandoricum indicum", "Strawberry pear" };  
  9.   
  10.         var val = from f in fruits  
  11.                                  select f;  
  12.   
  13.         Label1.Text = "Fruits List:....... <br />";  
  14.         foreach (string names in fruits)  
  15.         {  
  16.             Label1.Text += names.ToString() + "<br />";  
  17.         }  
  18.   
  19.         Label2.Text = "After Calling SingleOrDefault Operator With Condition [EndsWith('communis')]: " + val.SingleOrDefault(f => f.EndsWith("communis"));  
  20.         Label2.Text += "<br /><br />After Calling SingleOrDefault Operator With Condition [EndsWith('false')]: " + val.SingleOrDefault(f => f.EndsWith("false"));  
  21.     }  
  22. </script>  
  23.   
  24. <html xmlns="http://www.w3.org/1999/xhtml">  
  25. <head id="Head1" runat="server">  
  26.     <title>How to use Linq SingleOrDefault operator with condition</title>  
  27. </head>  
  28. <body>  
  29.     <form id="form1" runat="server">  
  30.     <div>  
  31.         <h2 style="color:DarkBlue; font-style:italic;">  
  32.             Linq Standard Query Operator: Element Operator - SingleOrDefault With Condition  
  33.             <br />How to get the single element of a sequence or a  
  34.             <br /> default value if no element is found  
  35.         </h2>  
  36.         <hr width="725" align="left" color="CornFlowerBlue" />  
  37.         <asp:Label  
  38.              ID="Label1"  
  39.              runat="server"  
  40.              Font-Size="X-Large"  
  41.              ForeColor="IndianRed"  
  42.              Font-Italic="true"  
  43.              >  
  44.         </asp:Label>  
  45.         <br />  
  46.         <asp:Label  
  47.              ID="Label2"  
  48.              runat="server"  
  49.              Font-Size="X-Large"  
  50.              ForeColor="CornflowerBlue"  
  51.              Font-Italic="true"  
  52.              >  
  53.         </asp:Label>  
  54.         <br /><br />  
  55.         <asp:Button   
  56.             ID="Button1"  
  57.             runat="server"  
  58.             OnClick="Button1_Click"  
  59.             Text="Test Linq Query Operator - SingleOrDefault"  
  60.             Height="45"  
  61.             Font-Bold="true"  
  62.             ForeColor="DodgerBlue"  
  63.             />  
  64.     </div>  
  65.     </form>  
  66. </body>  
  67. </html>  






Linq SingleOrDefault Operator - How to get the single element of a sequence or a default value if no element is found

Linq SingleOrDefault Operator - How to get the single element of a sequence or a default value if no element is found
LinqSingleOrDefaultOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Manilkara zapota", "Litchi chevelu", "Orange Frucht", "Pitahaya roja" };  
  9.   
  10.         var itemEndsWithFrucht = from f in fruits  
  11.                                  where f.EndsWith("Frucht")  
  12.                                  select f;  
  13.   
  14.         var itemEndsWithFalse = from f in fruits  
  15.                                 where f.EndsWith("False")  
  16.                                  select f;  
  17.   
  18.         Label1.Text = "Fruits List:....... <br />";  
  19.         foreach (string names in fruits)  
  20.         {  
  21.             Label1.Text += names.ToString() + "<br />";  
  22.         }  
  23.   
  24.         Label2.Text = "After Calling SingleOrDefault Operator [itemEndsWithFrucht]: " + itemEndsWithFrucht.SingleOrDefault();  
  25.         Label2.Text += "<br /><br />After Calling SingleOrDefault Operator [itemEndsWithFalse]: " + itemEndsWithFalse.SingleOrDefault();  
  26.     }  
  27. </script>  
  28.   
  29. <html xmlns="http://www.w3.org/1999/xhtml">  
  30. <head id="Head1" runat="server">  
  31.     <title>Linq SingleOrDefault Operator - How to get the single element of a sequence or a default value if no element is found</title>  
  32. </head>  
  33. <body>  
  34.     <form id="form1" runat="server">  
  35.     <div>  
  36.         <h2 style="color:DarkBlue; font-style:italic;">  
  37.             Linq Standard Query Operator: Element Operator - SingleOrDefault  
  38.             <br />How to get the single element of a sequence or a  
  39.             <br /> default value if no element is found  
  40.         </h2>  
  41.         <hr width="525" align="left" color="CornFlowerBlue" />  
  42.         <asp:Label  
  43.              ID="Label1"  
  44.              runat="server"  
  45.              Font-Size="X-Large"  
  46.              ForeColor="DarkOrchid"  
  47.              Font-Italic="true"  
  48.              >  
  49.         </asp:Label>  
  50.         <br />  
  51.         <asp:Label  
  52.              ID="Label2"  
  53.              runat="server"  
  54.              Font-Size="X-Large"  
  55.              ForeColor="CornflowerBlue"  
  56.              Font-Italic="true"  
  57.              >  
  58.         </asp:Label>  
  59.         <br /><br />  
  60.         <asp:Button   
  61.             ID="Button1"  
  62.             runat="server"  
  63.             OnClick="Button1_Click"  
  64.             Text="Test Linq Query Operator - SingleOrDefault"  
  65.             Height="45"  
  66.             Font-Bold="true"  
  67.             ForeColor="DodgerBlue"  
  68.             />  
  69.     </div>  
  70.     </form>  
  71. </body>  
  72. </html>  






How to use Linq Single operator with condition

How to use Linq Single operator with condition
LinqSingleOperatorWithCondition.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Dragon eye", "Fico comune", "Gewone peer", "Guayaba manzana", "Kiwi fruit" };  
  9.   
  10.         var val = from f in fruits  
  11.                   select f;  
  12.   
  13.         Label1.Text = "Fruits List:....... <br />";  
  14.         foreach (string names in fruits)  
  15.         {  
  16.             Label1.Text += names.ToString() + "<br />";  
  17.         }  
  18.   
  19.         Label2.Text = "After Calling Single Operator With Condition [StartsWith('Gewone')]: <br />";  
  20.         Label2.Text += val.Single(f => f.StartsWith("Gewone"));  
  21.     }  
  22. </script>  
  23.   
  24. <html xmlns="http://www.w3.org/1999/xhtml">  
  25. <head id="Head1" runat="server">  
  26.     <title>How to use Linq Single operator with condition</title>  
  27. </head>  
  28. <body>  
  29.     <form id="form1" runat="server">  
  30.     <div>  
  31.         <h2 style="color:DarkBlue; font-style:italic;">  
  32.             Linq Standard Query Operator: Element Operator - Single With Condition  
  33.             <br />How to get the single element of a sequence  
  34.         </h2>  
  35.         <hr width="650" align="left" color="CornFlowerBlue" />  
  36.         <asp:Label  
  37.              ID="Label1"  
  38.              runat="server"  
  39.              Font-Size="X-Large"  
  40.              ForeColor="OrangeRed"  
  41.              Font-Italic="true"  
  42.              >  
  43.         </asp:Label>  
  44.         <br />  
  45.         <asp:Label  
  46.              ID="Label2"  
  47.              runat="server"  
  48.              Font-Size="X-Large"  
  49.              ForeColor="SkyBlue"  
  50.              Font-Italic="true"  
  51.              >  
  52.         </asp:Label>  
  53.         <br /><br />  
  54.         <asp:Button   
  55.             ID="Button1"  
  56.             runat="server"  
  57.             OnClick="Button1_Click"  
  58.             Text="Test Linq Query Operator - Single"  
  59.             Height="45"  
  60.             Font-Bold="true"  
  61.             ForeColor="DodgerBlue"  
  62.             />  
  63.     </div>  
  64.     </form>  
  65. </body>  
  66. </html>  






Linq Single Operator - How to get the single element of a sequence

Linq Single Operator - How to get the single element of a sequence
LinqSingleOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Cambodian palm", "Burmese Grape", "Citrus maxima", "Custard apple" };  
  9.   
  10.         var itemEndsWithGrape = from f in fruits  
  11.                                            where f.EndsWith("Grape")  
  12.                                            select f;  
  13.   
  14.         Label1.Text = "Fruits List:....... <br />";  
  15.         foreach (string names in fruits)  
  16.         {  
  17.             Label1.Text += names.ToString() + "<br />";  
  18.         }  
  19.   
  20.         Label2.Text = "After Calling Single Operator: " + itemEndsWithGrape.Single();  
  21.     }  
  22. </script>  
  23.   
  24. <html xmlns="http://www.w3.org/1999/xhtml">  
  25. <head id="Head1" runat="server">  
  26.     <title>Linq Single Operator - How to get the single element of a sequence</title>  
  27. </head>  
  28. <body>  
  29.     <form id="form1" runat="server">  
  30.     <div>  
  31.         <h2 style="color:DarkBlue; font-style:italic;">  
  32.             Linq Standard Query Operator: Element Operator - Single  
  33.             <br />How to get the single element of a sequence  
  34.         </h2>  
  35.         <hr width="600" align="left" color="CornFlowerBlue" />  
  36.         <asp:Label  
  37.              ID="Label1"  
  38.              runat="server"  
  39.              Font-Size="X-Large"  
  40.              ForeColor="DarkOrchid"  
  41.              Font-Italic="true"  
  42.              >  
  43.         </asp:Label>  
  44.         <br />  
  45.         <asp:Label  
  46.              ID="Label2"  
  47.              runat="server"  
  48.              Font-Size="X-Large"  
  49.              ForeColor="SkyBlue"  
  50.              Font-Italic="true"  
  51.              >  
  52.         </asp:Label>  
  53.         <br /><br />  
  54.         <asp:Button   
  55.             ID="Button1"  
  56.             runat="server"  
  57.             OnClick="Button1_Click"  
  58.             Text="Test Linq Query Operator - Single"  
  59.             Height="45"  
  60.             Font-Bold="true"  
  61.             ForeColor="DodgerBlue"  
  62.             />  
  63.     </div>  
  64.     </form>  
  65. </body>  
  66. </html>  






Linq SequenceEqual Operator - How to compare two sequences to see if they are equivalent

Linq SequenceEqual Operator - How to compare two sequences to see if they are equivalent
LinqSequenceEqualOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruitsList1 = { "Citrus sinensis", "Diospyros blancoi", "Coconut palm" };  
  9.         string[] fruitsList2 = { "Jambosa jambos", "Korean mango", "Malabar plum" };  
  10.         string[] fruitsList3 = { "Citrus sinensis", "Diospyros blancoi", "Coconut palm" };  
  11.   
  12.         Label1.Text = "Fruits List 1:....... <br />";  
  13.         foreach (string names in fruitsList1)  
  14.         {  
  15.             Label1.Text += names.ToString() + "<br />";  
  16.         }  
  17.   
  18.         Label1.Text += "<br />Fruits List 2:....... <br />";  
  19.         foreach (string names in fruitsList2)  
  20.         {  
  21.             Label1.Text += names.ToString() + "<br />";  
  22.         }  
  23.   
  24.         Label1.Text += "<br />Fruits List 3:....... <br />";  
  25.         foreach (string names in fruitsList3)  
  26.         {  
  27.             Label1.Text += names.ToString() + "<br />";  
  28.         }  
  29.   
  30.         Label2.Text = "After Calling SequenceEqual Operator [fruitsList1.Equals(fruitsList2)]: " + fruitsList1.SequenceEqual(fruitsList2);  
  31.         Label2.Text += "<br />After Calling SequenceEqual Operator [fruitsList1.Equals(fruitsList3)]: " + fruitsList1.SequenceEqual(fruitsList3);  
  32.     }  
  33. </script>  
  34.   
  35. <html xmlns="http://www.w3.org/1999/xhtml">  
  36. <head id="Head1" runat="server">  
  37.     <title>Linq SequenceEqual Operator - How to compare two sequences to see if they are equivalent</title>  
  38. </head>  
  39. <body>  
  40.     <form id="form1" runat="server">  
  41.     <div>  
  42.         <h2 style="color:DarkBlue; font-style:italic;">  
  43.             Linq Standard Query Operator - SequenceEqual  
  44.             <br />How to compare two sequences to see if they are equivalent  
  45.         </h2>  
  46.         <hr width="575" align="left" color="CornFlowerBlue" />  
  47.         <asp:Label  
  48.              ID="Label1"  
  49.              runat="server"  
  50.              Font-Size="X-Large"  
  51.              ForeColor="SeaGreen"  
  52.              Font-Italic="true"  
  53.              >  
  54.         </asp:Label>  
  55.         <br />  
  56.         <asp:Label  
  57.              ID="Label2"  
  58.              runat="server"  
  59.              Font-Size="X-Large"  
  60.              ForeColor="DarkMagenta"  
  61.              Font-Italic="true"  
  62.              >  
  63.         </asp:Label>  
  64.         <br />  
  65.         <asp:Button   
  66.             ID="Button1"  
  67.             runat="server"  
  68.             OnClick="Button1_Click"  
  69.             Text="Test Linq Query Operator - SequenceEqual"  
  70.             Height="45"  
  71.             Font-Bold="true"  
  72.             ForeColor="DodgerBlue"  
  73.             />  
  74.     </div>  
  75.     </form>  
  76. </body>  
  77. </html>  






Linq Repeat Operator - How to generate a sequence by repeating an item a given number of times

Linq Repeat Operator - How to generate a sequence by repeating an item a given number of times
LinqRepeatOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         var repeatedText = Enumerable.Repeat("asp.net example",5);  
  9.   
  10.         Label1.Text = "Repeated Item....... <br />";  
  11.         foreach (string s in repeatedText)  
  12.         {  
  13.             Label1.Text += s.ToString() + "<br />";  
  14.         }  
  15.     }  
  16. </script>  
  17.   
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head id="Head1" runat="server">  
  20.     <title>Linq Repeat Operator - How to generate a sequence by repeating an item a given number of times</title>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <h2 style="color:DarkBlue; font-style:italic;">  
  26.             Linq Standard Query Operator  
  27.             <br /> Generation Operator - Repeat  
  28.             <br />How to generate a sequence by  
  29.             <br /> repeating an item a given number of times  
  30.         </h2>  
  31.         <hr width="425" align="left" color="CornFlowerBlue" />  
  32.         <asp:Label  
  33.              ID="Label1"  
  34.              runat="server"  
  35.              Font-Size="X-Large"  
  36.              ForeColor="DarkOrchid"  
  37.              Font-Italic="true"  
  38.              >  
  39.         </asp:Label>  
  40.         <br />  
  41.         <asp:Button   
  42.             ID="Button1"  
  43.             runat="server"  
  44.             OnClick="Button1_Click"  
  45.             Text="Test Linq Query Operator - Repeat"  
  46.             Height="45"  
  47.             Font-Bold="true"  
  48.             ForeColor="DodgerBlue"  
  49.             />  
  50.     </div>  
  51.     </form>  
  52. </body>  
  53. </html>  






Linq Range Operator - How to generate a sequence of numbers given a range

Linq Range Operator - How to generate a sequence of numbers given a range
LinqRangeOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         var nums = Enumerable.Range(100,10);  
  9.   
  10.         Label1.Text = "Sequence of numbers....... <br />";  
  11.         foreach (int number in nums)  
  12.         {  
  13.             Label1.Text += number.ToString() + "<br />";  
  14.         }  
  15.     }  
  16. </script>  
  17.   
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head id="Head1" runat="server">  
  20.     <title>Linq Range Operator - How to generate a sequence of numbers given a range</title>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <h2 style="color:DarkBlue; font-style:italic;">  
  26.             Linq Standard Query Operator: Generation Operator - Range  
  27.             <br />How to generate a sequence of numbers given a range  
  28.         </h2>  
  29.         <hr width="550" align="left" color="CornFlowerBlue" />  
  30.         <asp:Label  
  31.              ID="Label1"  
  32.              runat="server"  
  33.              Font-Size="X-Large"  
  34.              ForeColor="SeaGreen"  
  35.              Font-Italic="true"  
  36.              >  
  37.         </asp:Label>  
  38.         <br />  
  39.         <asp:Button   
  40.             ID="Button1"  
  41.             runat="server"  
  42.             OnClick="Button1_Click"  
  43.             Text="Test Linq Query Operator - Range"  
  44.             Height="45"  
  45.             Font-Bold="true"  
  46.             ForeColor="DodgerBlue"  
  47.             />  
  48.     </div>  
  49.     </form>  
  50. </body>  
  51. </html>  






Linq ElementAtOrDefault Operator - How to get the element at a given index in a sequence or a default value if the index is out of range

Linq ElementAtOrDefault Operator - How to get the element at a given index in a sequence or a default value if the index is out of range
LinqElementAtOrDefaultOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Anón", "Annona reticulata", "Atte", "Bramble fruit" };  
  9.   
  10.         string elementAtOrDefaultIndex1 = (from f in fruits  
  11.                                            select f).ElementAtOrDefault(1);  
  12.   
  13.         string elementAtOrDefaultIndex5 = (from f in fruits  
  14.                                            select f).ElementAtOrDefault(5);  
  15.   
  16.         Label1.Text = "Fruits List:....... <br />";  
  17.         foreach (string names in fruits)  
  18.         {  
  19.             Label1.Text += names.ToString() + "<br />";  
  20.         }  
  21.   
  22.         Label2.Text = "After Calling ElementAtOrDefault(1) Operator: " + elementAtOrDefaultIndex1;  
  23.         Label2.Text += "<br /><br />After Calling ElementAtOrDefault(5) Operator: " + elementAtOrDefaultIndex5;  
  24.     }  
  25. </script>  
  26.   
  27. <html xmlns="http://www.w3.org/1999/xhtml">  
  28. <head id="Head1" runat="server">  
  29.     <title>Linq ElementAtOrDefault Operator - How to get the element at a given index in a sequence or a default value if the index is out of range</title>  
  30. </head>  
  31. <body>  
  32.     <form id="form1" runat="server">  
  33.     <div>  
  34.         <h2 style="color:DarkBlue; font-style:italic;">  
  35.             Linq Standard Query Operator: Element Operator - ElementAtOrDefault  
  36.             <br />How to get the element at a given index in a sequence or  
  37.             <br /> a default value if the index is out of range  
  38.         </h2>  
  39.         <hr width="600" align="left" color="CornFlowerBlue" />  
  40.         <asp:Label  
  41.              ID="Label1"  
  42.              runat="server"  
  43.              Font-Size="X-Large"  
  44.              ForeColor="DarkOrchid"  
  45.              Font-Italic="true"  
  46.              >  
  47.         </asp:Label>  
  48.         <br />  
  49.         <asp:Label  
  50.              ID="Label2"  
  51.              runat="server"  
  52.              Font-Size="X-Large"  
  53.              ForeColor="MediumBlue"  
  54.              Font-Italic="true"  
  55.              >  
  56.         </asp:Label>  
  57.         <br /><br />  
  58.         <asp:Button   
  59.             ID="Button1"  
  60.             runat="server"  
  61.             OnClick="Button1_Click"  
  62.             Text="Test Linq Query Operator - ElementAtOrDefault"  
  63.             Height="45"  
  64.             Font-Bold="true"  
  65.             ForeColor="DodgerBlue"  
  66.             />  
  67.     </div>  
  68.     </form>  
  69. </body>  
  70. </html>  






Linq DefaultIfEmpty Operator - How to create a default element for an empty sequence

Linq DefaultIfEmpty Operator - How to create a default element for an empty sequence
LinqDefaultIfEmptyOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruits = { "Lusho fruit", "Cambodian palm", "Chinesische Haselnuss", "Jambosa jambos", "Chinesische Jujube" };  
  9.   
  10.         var itemStartWithC = from f in fruits  
  11.                              where f.StartsWith("C")  
  12.                              select f;  
  13.         var itemStartWithX = from f in fruits  
  14.                              where f.StartsWith("X")  
  15.                              select f;  
  16.   
  17.         Label1.Text = "Fruits List:....... <br />";  
  18.         foreach (string names in fruits)  
  19.         {  
  20.             Label1.Text += names.ToString() + "<br />";  
  21.         }  
  22.   
  23.         Label2.Text = "Fruits Start With(C):....... <br />";  
  24.         foreach (string names in itemStartWithC.DefaultIfEmpty("Default Value"))  
  25.         {  
  26.             Label2.Text += names.ToString() + "<br />";  
  27.         }  
  28.   
  29.         Label2.Text += "<br />Fruits Start With(X):....... <br />";  
  30.         foreach (string names in itemStartWithX.DefaultIfEmpty("Default Value"))  
  31.         {  
  32.             Label2.Text += names.ToString() + "<br />";  
  33.         }  
  34.     }  
  35. </script>  
  36.   
  37. <html xmlns="http://www.w3.org/1999/xhtml">  
  38. <head id="Head1" runat="server">  
  39.     <title>Linq DefaultIfEmpty Operator - How to create a default element for an empty sequence</title>  
  40. </head>  
  41. <body>  
  42.     <form id="form1" runat="server">  
  43.     <div>  
  44.         <h2 style="color:DarkBlue; font-style:italic;">  
  45.             Linq Standard Query Operator: Element Operator - DefaultIfEmpty  
  46.             <br />How to create a default element for an empty sequence  
  47.         </h2>  
  48.         <hr width="600" align="left" color="CornFlowerBlue" />  
  49.         <asp:Label  
  50.              ID="Label1"  
  51.              runat="server"  
  52.              Font-Size="X-Large"  
  53.              ForeColor="SeaGreen"  
  54.              Font-Italic="true"  
  55.              >  
  56.         </asp:Label>  
  57.         <br />  
  58.         <asp:Label  
  59.              ID="Label2"  
  60.              runat="server"  
  61.              Font-Size="X-Large"  
  62.              ForeColor="DarkMagenta"  
  63.              Font-Italic="true"  
  64.              >  
  65.         </asp:Label>  
  66.         <br />  
  67.         <asp:Button   
  68.             ID="Button1"  
  69.             runat="server"  
  70.             OnClick="Button1_Click"  
  71.             Text="Test Linq Query Operator - DefaultIfEmpty"  
  72.             Height="45"  
  73.             Font-Bold="true"  
  74.             ForeColor="DodgerBlue"  
  75.             />  
  76.     </div>  
  77.     </form>  
  78. </body>  
  79. </html>  






Linq Concat Operator - How to concatenate two sequences into one sequence

Linq Concat Operator - How to concatenate two sequences into one sequence
LinqConcatOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string[] fruitsStartsWithA = { "Actinidia deliciosa", "Albero del pane", "Annona muricata" };  
  9.         string[] fruitsStartsWithB = { "Brazilian pawpaw", "Bramble fruit" };  
  10.   
  11.         var allFruits = fruitsStartsWithA.Concat(fruitsStartsWithB);  
  12.   
  13.         Label1.Text = "Fruits Starts With 'A' List:....... <br />";  
  14.         foreach (string names in fruitsStartsWithA)  
  15.         {  
  16.             Label1.Text += names.ToString() + "<br />";  
  17.         }  
  18.   
  19.         Label1.Text += "<br />Fruits Starts With 'B' List:....... <br />";  
  20.         foreach (string names in fruitsStartsWithB)  
  21.         {  
  22.             Label1.Text += names.ToString() + "<br />";  
  23.         }  
  24.   
  25.         Label2.Text = "All Fruits After Calling Concat Operator:....... <br />";  
  26.         foreach (string names in allFruits)  
  27.         {  
  28.             Label2.Text += names.ToString() + "<br />";  
  29.         }  
  30.     }  
  31. </script>  
  32.   
  33. <html xmlns="http://www.w3.org/1999/xhtml">  
  34. <head id="Head1" runat="server">  
  35.     <title>Linq Concat Operator - How to concatenate two sequences into one sequence</title>  
  36. </head>  
  37. <body>  
  38.     <form id="form1" runat="server">  
  39.     <div>  
  40.         <h2 style="color:DarkBlue; font-style:italic;">  
  41.             Linq Standard Query Operator - Concat  
  42.             <br />How to concatenate two sequences into one sequence  
  43.         </h2>  
  44.         <hr width="525" align="left" color="CornFlowerBlue" />  
  45.         <asp:Label  
  46.              ID="Label1"  
  47.              runat="server"  
  48.              Font-Size="X-Large"  
  49.              ForeColor="SeaGreen"  
  50.              Font-Italic="true"  
  51.              >  
  52.         </asp:Label>  
  53.         <br />  
  54.         <asp:Label  
  55.              ID="Label2"  
  56.              runat="server"  
  57.              Font-Size="X-Large"  
  58.              ForeColor="DarkMagenta"  
  59.              Font-Italic="true"  
  60.              >  
  61.         </asp:Label>  
  62.         <br />  
  63.         <asp:Button   
  64.             ID="Button1"  
  65.             runat="server"  
  66.             OnClick="Button1_Click"  
  67.             Text="Test Linq Query Operator - Concat"  
  68.             Height="45"  
  69.             Font-Bold="true"  
  70.             ForeColor="DodgerBlue"  
  71.             />  
  72.     </div>  
  73.     </form>  
  74. </body>  
  75. </html>  






Linq TakeWhile Operator - How to get a sequence that takes items that meet an expression

Linq TakeWhile Operator - How to get a sequence that takes items that meet an expression
LinqTakeWhileOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         int[] nums = { 129,56,21,9,421,169,26,89 };  
  9.   
  10.         IEnumerable<int> val = nums.OrderBy(n => n).TakeWhile(n=> n<50);  
  11.   
  12.         Label1.Text = "Numbers List: <br />";  
  13.   
  14.         foreach (int i in nums)  
  15.         {  
  16.             Label1.Text += i.ToString() + "<br />";  
  17.         }  
  18.   
  19.         Label2.Text = "After Calling TakeWhile(number<50) Operator:<br />";  
  20.         foreach (int n in val)  
  21.         {  
  22.             Label2.Text += n.ToString() + "<br />";  
  23.         }  
  24.     }  
  25. </script>  
  26.   
  27. <html xmlns="http://www.w3.org/1999/xhtml">  
  28. <head id="Head1" runat="server">  
  29.     <title>Linq TakeWhile Operator - How to get a sequence that takes items that meet an expression</title>  
  30. </head>  
  31. <body>  
  32.     <form id="form1" runat="server">  
  33.     <div>  
  34.         <h2 style="color:DarkBlue; font-style:italic;">  
  35.             Linq Standard Query Operator: Partitioning Operator - TakeWhile  
  36.             <br />How to get a sequence that takes items that meet an expression  
  37.         </h2>  
  38.         <hr width="650" align="left" color="CornFlowerBlue" />  
  39.         <asp:Label  
  40.              ID="Label1"  
  41.              runat="server"  
  42.              Font-Size="X-Large"  
  43.              ForeColor="OrangeRed"  
  44.              Font-Italic="true"  
  45.              >  
  46.         </asp:Label>  
  47.         <br />  
  48.         <asp:Label  
  49.              ID="Label2"  
  50.              runat="server"  
  51.              Font-Size="X-Large"  
  52.              ForeColor="SaddleBrown"  
  53.              Font-Italic="true"  
  54.              >  
  55.         </asp:Label>  
  56.         <br />  
  57.         <asp:Button   
  58.             ID="Button1"  
  59.             runat="server"  
  60.             OnClick="Button1_Click"  
  61.             Text="Test Linq Query Operator - TakeWhile"  
  62.             Height="45"  
  63.             Font-Bold="true"  
  64.             ForeColor="DodgerBlue"  
  65.             />  
  66.     </div>  
  67.     </form>  
  68. </body>  
  69. </html>  






Linq Take Operator - How to get a sequence that takes a given number of items

Linq Take Operator - How to get a sequence that takes a given number of items
LinqTakeOperator.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2. <%@ Import Namespace="System.Linq" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         int[] nums = { 255,21,455,41,233,169,26,123 };  
  9.   
  10.         IEnumerable<int> val = nums.OrderByDescending(n => n).Take(3);  
  11.   
  12.         Label1.Text = "Numbers List: <br />";  
  13.   
  14.         foreach (int i in nums)  
  15.         {  
  16.             Label1.Text += i.ToString() + "<br />";  
  17.         }  
  18.   
  19.         Label2.Text = "After Calling Take(3) Operator: Top 3 numbers<br />";  
  20.         foreach (int n in val)  
  21.         {  
  22.             Label2.Text += n.ToString() + "<br />";  
  23.         }  
  24.     }  
  25. </script>  
  26.   
  27. <html xmlns="http://www.w3.org/1999/xhtml">  
  28. <head id="Head1" runat="server">  
  29.     <title>Linq Take Operator - How to get a sequence that takes a given number of items</title>  
  30. </head>  
  31. <body>  
  32.     <form id="form1" runat="server">  
  33.     <div>  
  34.         <h2 style="color:DarkBlue; font-style:italic;">  
  35.             Linq Standard Query Operator: Partitioning Operator - Take  
  36.             <br />How to get a sequence that takes a given number of items  
  37.         </h2>  
  38.         <hr width="600" align="left" color="CornFlowerBlue" />  
  39.         <asp:Label  
  40.              ID="Label1"  
  41.              runat="server"  
  42.              Font-Size="X-Large"  
  43.              ForeColor="DarkMagenta"  
  44.              Font-Italic="true"  
  45.              >  
  46.         </asp:Label>  
  47.         <br />  
  48.         <asp:Label  
  49.              ID="Label2"  
  50.              runat="server"  
  51.              Font-Size="X-Large"  
  52.              ForeColor="DarkCyan"  
  53.              Font-Italic="true"  
  54.              >  
  55.         </asp:Label>  
  56.         <br />  
  57.         <asp:Button   
  58.             ID="Button1"  
  59.             runat="server"  
  60.             OnClick="Button1_Click"  
  61.             Text="Test Linq Query Operator - Take"  
  62.             Height="45"  
  63.             Font-Bold="true"  
  64.             ForeColor="DodgerBlue"  
  65.             />  
  66.     </div>  
  67.     </form>  
  68. </body>  
  69. </html>  




 

No comments:

Post a Comment