Mattlinux1 Mattlinux1 - 2 years ago 95
SQL Question

Having problems converting a complicated LINQ DataContext C# query to a SQL query

I have currently been trying to convert the query string/code below to a SQL query.

If anyone can decode the query, I would be grateful.

AtrDataContext orion = new AtrDataContext();
MasterCalculator dcc = (from a in orion.MasterCalculator
where DaysOld >= a.startDayRange
&& (!a.endDayRange.HasValue || DaysOld <= a.endDayRange)
select a).FirstOrDefault();

Answer Source

The HasValue is just a null checker. DaysOld seems like it is a variable passed in. Something like this...

FROM MasterCalculator a
WHERE a.startDayRange <= @DaysOld
    AND (a.endDayRange == NULL OR  a.endDayRange >= @DaysOld)

Of course, good practice is to specify each of the columns

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download