Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles CA and Austin TX. Follow galratner on Twitter Google
Access the first object in a GroupBy with LinqDataSource

LinqDataSource has a great option of accessing the groups retured as a GroupBy Linq query. Including “it” in the query will add a property in the anonymous type retured from the query containing a collection of the groups themselfs.

<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" GroupBy="MemberID" OrderBy="TimeDate desc"
Select="new (key as MemberID, it as Status, Max(TimeDate) as Max_TimeDate)" TableName="Status"></asp:LinqDataSource>

Accessing the first group only will require us to add a FormView to the Repeater item and asign “it” as its data source.

<asp:Repeater ID="SummaryRepeater" runat="server" DataSourceID ="LinqDataSource1"><ItemTemplate>
<asp:FormView ID="FormView1" DataSource='<%# Eval("Status")%>' runat="server"><ItemTemplate><%# Eval("StatusName")%></ItemTemplate></asp:FormView>

You can access the rest of the collection simply by adding a ListView.
More on grouping with LinqDataSource here

Posted 6 Sep 2009 7:20 PM by Gal Ratner
Filed under: ,

Powered by Community Server (Non-Commercial Edition), by Telligent Systems