Mattlinux1 Mattlinux1 - 6 months ago 28
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

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

SELECT TOP 1 *
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

Comments