将SQL转换为LINQ查询

时间:2016-07-20 10:49:15

标签: sql linq linq-to-sql lambda

我想将此查询转换为LINQ或lambda表达式

    ALTER procedure  [dbo].[grid_data]
   @fromdate datetime,
   @todate  datetime,
   @region   varchar(50)
    as
    Select D.ID as ID, D.OName,  
    (Select Count(*) from tblve WHERE MID = D.ID and Vme <> ''),  
    D.Mil,D.Speed  
    from tblRe M  
    inner join tblReg D  
    On M.RID = D.RID  

我尝试这种方法,但这显示计数错误

[WebMethod]
     public static string search_data(DateTime fromdate, DateTime todate, string region)
    { 
        try
        {  
          T1 ts = new T1();
          var query = (from M in ts.tblRe 
          join D in ts.tblReg on M.RID equals D.RID
          where 
          M.Region == region
          && M.StartDate <= fromdate
          && M.EndDate >= todate
          select new {                      
          D.ID,
          D.OName,
          Count = ts.tblve.Where(x => x.MID == D.ID && x.Vme !=  
          '').Count()
          D.Mil,
          D.Speed
         }).ToList();

这显示出错误     空字符文字 在这一行'')

1 个答案:

答案 0 :(得分:2)

这个解决方案怎么样:

  var query = from M in context.tblRe 
              join D in context.tblReg on M.RID equals D.RID
              where M.StartDate <= fromdate && M.EndDate >= todate && M.region == region 
              select new {                      
                  D.ID,
                  D.OName,
                  Count = context.tblve.Where(x => x.MID == D.ID && x.Vme != "").Count()
                  D.Mil,
                  D.Speed
              };