SQL语句转换为LINQ?

时间:2015-01-29 03:42:48

标签: c# sql linq

好的......所以我有一个可用的下拉列表,但我不会加入更多支票....在asp格式我设法检查日期限制但我需要从另一个表的结果,但在ASP不知道如何计算....他已经制作的ASP版本,我已经做了一个SQL On来获得一个想法

这是LINQ版本唯一不起作用的是已分配到时间表的门票数量

var query = from table in db.Timetables
                        join tick in db.Tickets on table.ID equals tick.TimetableID
                        where table.Schedual_Date > DateTime.Today 
                        && table.Advance_Tickets > "NEEDS TO GET NUMBER OF TICKETS ASSIGNED TO TIMETABLE"
                        select table;

SQL VERSION

 SELECT 
 COUNT(Tickets.TimetableID) AS NoTickets, * FROM Tickets 
 JOIN Timetable ON Tickets.TimetableID=Timetable.ID
 Where timetable.Schedual_Date > (TODAYS DATE) 
 && Timetable.Advance_Tickets > NoTickets

2 个答案:

答案 0 :(得分:0)

使用.Count()函数获取计数。在你的情况下tick.Count()会有所帮助。例如,

var query = from table in db.Timetables
                    join tick in db.Tickets on table.ID equals tick.TimetableID
                    where table.Schedual_Date > DateTime.Today 
                    && table.Advance_Tickets > tick.Count()
                    select table;

答案 1 :(得分:0)

如果你正在寻找SQLQ查询的LINQ等价,如下所示

SELECT * FROM
Timetable Timetable 
Where timetable.Schedual_Date > getDate() 
AND Timetable.Advance_Tickets >= (SELECT COUNT(1) FROM Tickets Tickets WHERE Tickets.TimetableID=Timetable.ID)

你可以尝试

    var query = from table in timeTables                        
                where table.SchedualDate > DateTime.Today
                && table.AdvanceTickets > (tickets.Count(a => a.TimeTableId == table.Id))
                select table;