tags:

views:

39

answers:

3

I want to receive the newest item from a collection. Each item has an DateTime field (EditDate) and I do a little query like that.

var s = from l in OCollectionAgents
    where l.IDOfVehicle == agent.IDOfVehicle
    orderby
    agent.EditDate ascending
    select l;

After the query I do

agent.DetailInformationOfResults.NewestAgentEditDate = s.First().EditDate;

But no matter if is set the sort direction to ascending or descending I always get the same item. The oldest item.

I fixed the problem by doing

agent.DetailInformationOfResults.NewestAgentEditDate = s.Max(d => d.EditDate);

This is a solution but I still wonder why my query result does not change the sort direction.

A: 

May be you need to order by l.EditDate ?

Orsol
+4  A: 

In your query agent.EditDate is a constant expression and will be the same for every item. Perhaps you want a join?

Mark Byers
This is the problem - that you're not sorting the selected items.
Murph
Yes .... sometimes its very easy to make a fool out of yourself. Thanks a lot.
Holli
+1  A: 

Both answers from mark and orsol look correct. I think you want to do following

var s = from l in OCollectionAgents
    where l.IDOfVehicle == agent.IDOfVehicle
    orderby
    l.EditDate ascending
    select l;
affan