2013-05-23 3 views
0

У меня есть инструкция Sql, для которой мне нужно сгенерировать соответствующее выражение Лямбды (Linq). Вот SQLКак написать выражение lambda (linq) для sql?

Declare @CurrentUserId as int 
Declare @CurrentDate as datetime 
Set @CurrentDate = GetDate() 
Set @CurrentUserId = 1 

Select C.conferenceId,C.specialtyId,C.name,C.city,S.abbr as statebbbr,CTRY.name as countryname,C.startdate,C.enddate 
from Conferences C 
Inner join (
    Select distinct SpecialtyId 
    from UserContent 
    Where UserId = @CurrentUserId and DeletedFlag = 0 
    ) DT on C.SpecialtyId = DT.SpecialtyId 
Left outer join State S on C.StateId = S.StateId 
Inner join Country CTRY on C.CountryId = CTRY.CountryId 
Where C.DisplayStartDate <= @CurrentDate 
    and C.DisplayEndDate >= @CurrentDate 
    and C.DeletedFlag = 0 
    and C.Publish = 1 
Order by C.startdate ASC 

Что wolud быть (LINQ) лямбда-выражения для этого?

ответ

1

Пусть контекст данных в переменной context

from c in context.conferences 
join ctry in context.country on c.CountryId equals ctry.CountryId 
join s1 in context.State on c.StateId equals s.StateId into s2 
from s in s2.DefaultIfEmpty() 
where 
    c.DisplayStartDate <= System.DateTime.Now 
    && c.DisplayEndDate >= System.DateTime.Now 
    && c.DeletedFlag == 0 // or false if represented as a bool 
    && c.Publish == 1 // or true if represented as a bool 
    && context.UserContent.Any(
           x => x.SpecialityId == c.specialityId 
           && x.UserId == currentUserId 
           && x.DeletedFlag == 0 
           // or if represented as a bool "&& !x.DeletedFlag" 
          ) 
select new { 
     c.ConferenceId, 
     c.SpecialtyId, 
     c.name, 
     c.city, 
     stateabbr = s.abbr, 
     countryname = ctry.name, 
     c.startdate, 
     c.enddate 
} 
Смежные вопросы