How to use Linq SingleOrDefault operator with condition
LinqSingleOrDefaultOperatorWithCondition.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 = { "Zoete sinaasappel", "Pyrus communis", "Sandoricum indicum", "Strawberry pear" };
- var val = from f in fruits
- select f;
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling SingleOrDefault Operator With Condition [EndsWith('communis')]: " + val.SingleOrDefault(f => f.EndsWith("communis"));
- Label2.Text += "<br /><br />After Calling SingleOrDefault Operator With Condition [EndsWith('false')]: " + val.SingleOrDefault(f => f.EndsWith("false"));
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>How to use Linq SingleOrDefault operator with condition</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - SingleOrDefault With Condition
- <br />How to get the single element of a sequence or a
- <br /> default value if no element is found
- </h2>
- <hr width="725" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="IndianRed"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="CornflowerBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - SingleOrDefault"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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 = { "Manilkara zapota", "Litchi chevelu", "Orange Frucht", "Pitahaya roja" };
- var itemEndsWithFrucht = from f in fruits
- where f.EndsWith("Frucht")
- select f;
- var itemEndsWithFalse = from f in fruits
- where f.EndsWith("False")
- select f;
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling SingleOrDefault Operator [itemEndsWithFrucht]: " + itemEndsWithFrucht.SingleOrDefault();
- Label2.Text += "<br /><br />After Calling SingleOrDefault Operator [itemEndsWithFalse]: " + itemEndsWithFalse.SingleOrDefault();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq SingleOrDefault Operator - How to get the single element of a sequence or a default value if no element is found</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - SingleOrDefault
- <br />How to get the single element of a sequence or a
- <br /> default value if no element is found
- </h2>
- <hr width="525" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkOrchid"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="CornflowerBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - SingleOrDefault"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


How to use Linq Single operator with condition
How to use Linq Single operator with condition
LinqSingleOperatorWithCondition.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 = { "Dragon eye", "Fico comune", "Gewone peer", "Guayaba manzana", "Kiwi fruit" };
- var val = from f in fruits
- select f;
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling Single Operator With Condition [StartsWith('Gewone')]: <br />";
- Label2.Text += val.Single(f => f.StartsWith("Gewone"));
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>How to use Linq Single operator with condition</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - Single With Condition
- <br />How to get the single element of a sequence
- </h2>
- <hr width="650" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="OrangeRed"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="SkyBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Single"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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 = { "Cambodian palm", "Burmese Grape", "Citrus maxima", "Custard apple" };
- var itemEndsWithGrape = from f in fruits
- where f.EndsWith("Grape")
- select f;
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling Single Operator: " + itemEndsWithGrape.Single();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Single Operator - How to get the single element of a sequence</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - Single
- <br />How to get the single element of a sequence
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkOrchid"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="SkyBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Single"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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[] fruitsList1 = { "Citrus sinensis", "Diospyros blancoi", "Coconut palm" };
- string[] fruitsList2 = { "Jambosa jambos", "Korean mango", "Malabar plum" };
- string[] fruitsList3 = { "Citrus sinensis", "Diospyros blancoi", "Coconut palm" };
- Label1.Text = "Fruits List 1:....... <br />";
- foreach (string names in fruitsList1)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label1.Text += "<br />Fruits List 2:....... <br />";
- foreach (string names in fruitsList2)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label1.Text += "<br />Fruits List 3:....... <br />";
- foreach (string names in fruitsList3)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling SequenceEqual Operator [fruitsList1.Equals(fruitsList2)]: " + fruitsList1.SequenceEqual(fruitsList2);
- Label2.Text += "<br />After Calling SequenceEqual Operator [fruitsList1.Equals(fruitsList3)]: " + fruitsList1.SequenceEqual(fruitsList3);
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq SequenceEqual Operator - How to compare two sequences to see if they are equivalent</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator - SequenceEqual
- <br />How to compare two sequences to see if they are equivalent
- </h2>
- <hr width="575" 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 />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - SequenceEqual"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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)
- {
- var repeatedText = Enumerable.Repeat("asp.net example",5);
- Label1.Text = "Repeated Item....... <br />";
- foreach (string s in repeatedText)
- {
- Label1.Text += s.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Repeat Operator - How to generate a sequence by repeating an item a given number of times</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator
- <br /> Generation Operator - Repeat
- <br />How to generate a sequence by
- <br /> repeating an item a given number of times
- </h2>
- <hr width="425" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkOrchid"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Repeat"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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)
- {
- var nums = Enumerable.Range(100,10);
- Label1.Text = "Sequence of numbers....... <br />";
- foreach (int number in nums)
- {
- Label1.Text += number.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Range Operator - How to generate a sequence of numbers given a range</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Generation Operator - Range
- <br />How to generate a sequence of numbers given a range
- </h2>
- <hr width="550" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="SeaGreen"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Range"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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 = { "Anón", "Annona reticulata", "Atte", "Bramble fruit" };
- string elementAtOrDefaultIndex1 = (from f in fruits
- select f).ElementAtOrDefault(1);
- string elementAtOrDefaultIndex5 = (from f in fruits
- select f).ElementAtOrDefault(5);
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "After Calling ElementAtOrDefault(1) Operator: " + elementAtOrDefaultIndex1;
- Label2.Text += "<br /><br />After Calling ElementAtOrDefault(5) Operator: " + elementAtOrDefaultIndex5;
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <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>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - ElementAtOrDefault
- <br />How to get the element at a given index in a sequence or
- <br /> a default value if the index is out of range
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkOrchid"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="MediumBlue"
- Font-Italic="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - ElementAtOrDefault"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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 = { "Lusho fruit", "Cambodian palm", "Chinesische Haselnuss", "Jambosa jambos", "Chinesische Jujube" };
- var itemStartWithC = from f in fruits
- where f.StartsWith("C")
- select f;
- var itemStartWithX = from f in fruits
- where f.StartsWith("X")
- select f;
- Label1.Text = "Fruits List:....... <br />";
- foreach (string names in fruits)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "Fruits Start With(C):....... <br />";
- foreach (string names in itemStartWithC.DefaultIfEmpty("Default Value"))
- {
- Label2.Text += names.ToString() + "<br />";
- }
- Label2.Text += "<br />Fruits Start With(X):....... <br />";
- foreach (string names in itemStartWithX.DefaultIfEmpty("Default Value"))
- {
- Label2.Text += names.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq DefaultIfEmpty Operator - How to create a default element for an empty sequence</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Element Operator - DefaultIfEmpty
- <br />How to create a default element for an empty sequence
- </h2>
- <hr width="600" 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 />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - DefaultIfEmpty"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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[] fruitsStartsWithA = { "Actinidia deliciosa", "Albero del pane", "Annona muricata" };
- string[] fruitsStartsWithB = { "Brazilian pawpaw", "Bramble fruit" };
- var allFruits = fruitsStartsWithA.Concat(fruitsStartsWithB);
- Label1.Text = "Fruits Starts With 'A' List:....... <br />";
- foreach (string names in fruitsStartsWithA)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label1.Text += "<br />Fruits Starts With 'B' List:....... <br />";
- foreach (string names in fruitsStartsWithB)
- {
- Label1.Text += names.ToString() + "<br />";
- }
- Label2.Text = "All Fruits After Calling Concat Operator:....... <br />";
- foreach (string names in allFruits)
- {
- Label2.Text += names.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Concat Operator - How to concatenate two sequences into one sequence</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator - Concat
- <br />How to concatenate two sequences into one sequence
- </h2>
- <hr width="525" 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 />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Concat"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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[] nums = { 129,56,21,9,421,169,26,89 };
- IEnumerable<int> val = nums.OrderBy(n => n).TakeWhile(n=> n<50);
- Label1.Text = "Numbers List: <br />";
- foreach (int i in nums)
- {
- Label1.Text += i.ToString() + "<br />";
- }
- Label2.Text = "After Calling TakeWhile(number<50) Operator:<br />";
- foreach (int n in val)
- {
- Label2.Text += n.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq TakeWhile Operator - How to get a sequence that takes items that meet an expression</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Partitioning Operator - TakeWhile
- <br />How to get a sequence that takes items that meet an expression
- </h2>
- <hr width="650" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="OrangeRed"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="SaddleBrown"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - TakeWhile"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </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
- <%@ 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[] nums = { 255,21,455,41,233,169,26,123 };
- IEnumerable<int> val = nums.OrderByDescending(n => n).Take(3);
- Label1.Text = "Numbers List: <br />";
- foreach (int i in nums)
- {
- Label1.Text += i.ToString() + "<br />";
- }
- Label2.Text = "After Calling Take(3) Operator: Top 3 numbers<br />";
- foreach (int n in val)
- {
- Label2.Text += n.ToString() + "<br />";
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Linq Take Operator - How to get a sequence that takes a given number of items</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:DarkBlue; font-style:italic;">
- Linq Standard Query Operator: Partitioning Operator - Take
- <br />How to get a sequence that takes a given number of items
- </h2>
- <hr width="600" align="left" color="CornFlowerBlue" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkMagenta"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Label
- ID="Label2"
- runat="server"
- Font-Size="X-Large"
- ForeColor="DarkCyan"
- Font-Italic="true"
- >
- </asp:Label>
- <br />
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Test Linq Query Operator - Take"
- Height="45"
- Font-Bold="true"
- ForeColor="DodgerBlue"
- />
- </div>
- </form>
- </body>
- </html>


No comments:
Post a Comment