tags:

views:

393

answers:

4

Hello all,

Is there a online system which converts SQL - LINQ or can anyone else help convert the SQL - LINQ below?

SELECT MIN(startTime) As startTime, MAX(endTime) As endTime
FROM tblRA
LEFT JOIN tblA ON tblRA.asID = tblA.asID
WHERE 'xxxxxx' BETWEEN tblRA.startDate AND tblRA.endDate
AND tblA.availabilityDayOfWeek = 7

The main area I am having trouble is the .MAX/.MIN.

Heres what I have so far

    public List<string> GetResourceAvailabilitiesByDate(DateTime searchDate)
    {
        DayOfWeek dayOfWeek = searchDate.DayOfWeek;

        var minVal = from a in dc.tblResourceAvailabilities
                join b in dc.tblAvailabilities on a.asID equals b.asID 
                where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
                && b.availabilityDayOfWeek == (int)dayOfWeek
                select b.startTime.ToShortTimeString();;

        var maxVal = from a in dc.tblResourceAvailabilities
                     join b in dc.tblAvailabilities on a.asID equals b.asID
                     where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
                     && b.availabilityDayOfWeek == (int)dayOfWeek
                     select b.endTime.ToShortTimeString();

        var min = minVal.Min(minVal.Min);
        var max = maxVal.Max();

        return min,max;

Thanks in advance for any help

Clare

+1  A: 

Try this: http://www.sqltolinq.com/

Danny
I have looked at this before but have found that it isn't free
ClareBear
nor is it online; must be downloaded.
Michael Paulukonis
+1  A: 

I didn't find an example in the end, so decided to do it within a stored procedure.

Clare

ClareBear
Sorry meant to be a comment not a answer
ClareBear
A: 

You may find the Linq in Action book helpful.

Ian Ringrose
+1  A: 

I think your code is a little bit incorrect, and the first symptom of it is that you are using repeated code to define minval and maxval. I tried to simulate something similar to what you want and came to the following code, please adapt it to your needs.

    public List<string> GetResourceAvailabilitiesByDate(DateTime searchDate)
    {
        DayOfWeek dayOfWeek = searchDate.DayOfWeek;

        var vals  = from a in dc.tblResourceAvailabilities
            join b in dc.tblAvailabilities on a.asID equals b.asID 
            where searchDate.Date >= a.startDate.Date && searchDate.Date <= a.endDate.Value.Date
            && b.availabilityDayOfWeek == (int)dayOfWeek
            select b;

        var min = vals.Min(v => v.startTime).ToShortTimeString();
        var max = vals.Max(v => v.startTime).ToShortTimeString();

        return new List<string>() { min, max };
     }

Some comments on your code, assuming it's C#.

  1. You are trying to return an array of strings when you should be returning an array of dates.
  2. Your where clause is pretty confuse. You're comparing the search date with startdate.Date and endDate.Value.Date. It does not make much sense.
  3. Your select clause could select only b, or a, or whatever. You don't really need to select the date in it.
Paulo Guedes
I have added the endDate.Value.Date because this is allowed to be null. I only need to date so I didnt want to bring back data that wouldnt be used.
ClareBear
v.StartTime is a DateTime whereas I just need the Time, how would I do that?
ClareBear
ok, updated my answer.
Paulo Guedes