johnny 5 johnny 5 - 1 year ago 81
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 Source
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;   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download