开发者

Linq query with Datetime Object

开发者 https://www.devze.com 2022-12-16 14:30 出处:网络
I want to receive the newest item from a collection. Each item has a \"DateTime\" field (EditDate) and I do a little query like this:

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

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

Then after the query I do this:

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

But no matter what, if the sor开发者_如何学Got direction is ascending or descending I still get the same item. The oldest item.

I fixed the problem by doing this, and it worked.

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

But now, I wonder why does my query result not change the sort direction?


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


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;


May be you need to order by l.EditDate ?


Yes you have to try like this.

var s = from l in OCollectionAgents

where l.IDOfVehicle == agent.IDOfVehicle
orderby
l.EditDate ascending
select l;
0

精彩评论

暂无评论...
验证码 换一张
取 消