Dov Miller Dov Miller - 10 months ago 53
ASP.NET (C#) Question

How to use First Method in querying data with Entity Datasource?

I have the following code:

var orders = context.Orders
.GroupBy(i => i.Clients.ClientName)
.Select(i => i.OrderByDescending(it => it.OrderDate).FirstOrDefault());

I want to get only the last order made by each client. The basis for the code I got from here Remove duplicates in the list using linq, from Freddy's answer. (I'm including "Clients" because Orders has ClientId but not client name and the results are to be displayed in a grid including the client name which I'm getting from Clients).
This works properly.


Is it possible to do this using an Entity Datasource control?

Is it possible to use FirstOrDefault in some way in the Entity Datasource control?


I found here that with the EntityDataSource you can use:

Select="top(1) it.[ClientID]"

However if you want to Order by DESC the top will be executed before the Order by DESC. If you want the Order by executed before the top, in other words to get the last Item, instead of top do this in Code behind:

protected void entityDataSource_Selecting(object sender,EntityDataSourceSelectingEventArgs e)
    e.SelectArguments.MaximumRows = 1;

All of this I got from that link in the Qustion and Answer.