How to use Linq Last Operator with condition
LinqLastOperatorWithCondition.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 = { "Grenadier", "Fraisier", "Banane", "Common fig", "Borassus flabellifer", "Chicozapote" };
string lastItem = (from f in fruits
select f).Last(f=>f.StartsWith("B"));
Label1.Text = "Fruits List: <br />";
foreach (string names in fruits)
{
Label1.Text += names.ToString() + "<br />";
}
Label2.Text = "After Calling Last Operator With Condition [StartsWith('B')]: <br />" + lastItem;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to use Linq Last Operator with condition</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
Linq Standard Query Operator
<br /> Element Operator - Last With Condition
<br />How to get the last element of a sequence
</h2>
<hr width="450" align="left" color="CornFlowerBlue" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="X-Large"
ForeColor="Navy"
Font-Italic="true"
>
</asp:Label>
<br />
<asp:Label
ID="Label2"
runat="server"
Font-Size="X-Large"
ForeColor="DeepPink"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test Linq Query Operator - Last"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>

- linq - How to determine if any items in a sequence match condition
- linq - How to determine if a sequence contains a specific element
- linq - How to get a sequence without duplicate items
- 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 - Get first element of a sequence that match condition
- linq - How to get first element of a sequence or a default value
- linq - How to get last element of a sequence or a default value
- linq - Get last element of a sequence that match condition or a default value
- linq - How to take a given number of elements from a sequence