开发者

Best logic to select/restrict correct date from range - .net

Apologies for using someone else's brain for this, but I'm sure this is a common problem and has some sort of design pattern solution that I've not come across before.

I have an IList 开发者_开发问答of objects that each have a "start" and a "stop" date. These date ranges can overlap. What I need to do is select which object is current for today's date. If there's an overlapping date then I need to select the one that has a start date closest to today's date which which the stop date hasn't passed.

I haven't used Linq much but I get the impression it might be ideal for this kind of task.

Can someone point me in the right direction as to how to get the result required?

Cheers, Matt


var target = lst.Where(i => i.start <= DateTime.Now && i.end > DateTime.Now).OrderByDescending(i => i.start).First()

Eh... not much else to say.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜