Search code examples
c#asp.netlinqlinqdatasource

joining statement with linqdatasource


how can I make this sql query works with linqdatasource?

select tbl_WeekDays.WeekDay, tbl_DayTimes.TimeFrom, tbl_DayTimes.TimeTo from tbl_WeekDays

left join tbl_DayTimes on tbl_DayTimes.WeekDayId = tbl_WeekDays.WeekDayId
where tbl_WeekDays.classID = @id

I've read this answer but I couldn't understand the syntax used by linqdatasource join statement. any help would be great.


Solution

  • With a LinQ statement it should look something like following code. Please mind that this is written out of my head and not tested. It's a mere indication of how it should look, not a working solution.

    //Change following variables accordingly
    var ctx = new YourDbContext();
    var id = 3;
    
    from tblWeekDays in ctx.WeekDays
    join tblDayTimes in ctx.DayTimes
    on tblWeekDays.WeekDayId == tblDayTimes.WeekDayId
    where tblWeekDays.classID == id
    select new
    {
        WeekDay = tblWeekDays.WeekDay,
        TimeFrom = tblDayTimes.TimeFrom,
        TimeTo = tblDayTimes.TimeTo
    };
    

    Hope this gets you on the way.