Linq ElementAt Operator - How to get the element at a given index in a sequence
LinqElementAtOperator.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Linq" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string[] fruits = { "Fresa", "Feige", "Grosella espinosa", "Jujube", "Kraton", "Longkong" };
string elementAt2 = (from f in fruits
select f).ElementAt(2);
Label1.Text = "Fruits List:....... <br />";
foreach (string names in fruits)
{
Label1.Text += names.ToString() + "<br />";
}
Label2.Text = "After Calling ElementAt(2) Operator: " + elementAt2;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Linq ElementAt Operator - How to get the element at a given index in a sequence</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
Linq Standard Query Operator: Element Operator - ElementAt
<br />How to get the element at a given index in a sequence
</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="MediumBlue"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Linq Query Operator - ElementAt"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- linq - How to count items of a sequence
- linq - How to sort elements of a sequence in ascending order
- linq - How to determine if any items in a sequence match condition
- linq - How to get a sequence that skips a given number of items
- linq - Get a subset of sequence that skips unmatched condition items
- linq - How to get the first element of a sequence
- linq - How to determine whether two sequences are equal
- linq - How to get the single element of a sequence
- linq - How to create an XML element
- linq - How to count occurrences of a string within a string