johnny 5 johnny 5 - 2 days ago 5
C# Question

Linq Select items where they are before a certain date or the first after

I have a Linq To Entities Query which grabs all the items before a certain date, I was wondering if anyone body knows a way to also grab the first record larger than that date in the same query.

context.Items.Where(x => x.CheckDate < DateTime.Now)


Is there a way to modify this so that I can grab the first date after without making two queryies?

Answer
var items = from u in context.Items
                                 let dt = (from i in context.Items
                                  where i.CheckDate> DateTime.Now orderby i.CheckDate ).FirstOrDefault()
                                 where u.CheckDate <=dt
                             select u;   
Comments