2012-08-14 68 views
1

我想列出CloseDate大於DateTime.Now的所有記錄。有關日期的LINQ查詢問題

我已經爲該查詢寫了一個查詢,但它沒有提取記錄,其中CloseDate大於DateTime.Now
見下面

var query = (from x in objEntity.VacancyMsts 
        join o in objEntity.OrganizationMst on 
        x.OrganizationID equals o.OrganizationId into vacorg 
        from o in vacorg.DefaultIfEmpty() 
        where x.Status == true && x.CloseDate >= DateTime.Now 
        select new VacancyMstDTO 
        {}); 

我詢問是否有我的查詢任何問題嗎?

+0

什麼SQL針對數據庫執行? – 2012-08-14 12:29:05

+0

返回recots ..我的SQL查詢SELECT dbo.VacancyMst.PositionTitle,dbo.VacancyMst.Description,dbo.VacancyMst.PositionLocation,dbo.OrganizationMst.OrganizationName, dbo.VacancyMst.CloseDate FROM dbo.VacancyMst LEFT OUTER JOIN dbo.OrganizationMst ON dbo.VacancyMst.OrganizationID = dbo.OrganizationMst.OrganizationId WHERE(dbo.VacancyMst.Status = 1)AND(dbo.VacancyMst.CloseDate> = GETDATE()) – 2012-08-14 12:29:55

+0

@Manatherin是兩條記錄在那裏 – 2012-08-14 12:30:29

回答

0

你不需要在這裏加入,如果你有一個FK關係

var query = from x in objEntity.VacancyMsts 
      where x.Status && x.CloseDate >= DateTime.Now 
      select new VacancyMstDTO 
      { 
       OrganizationName = x.OrganizationMst.Name, 
       ... 
      }; 

不知,如果你真的想CloseDate要提前到今天,通常一個結束日期發生在過去,沒有?

而且,小心以及時區。我傾向於始終使用DateTime.UtcNow插入/更新數據庫,並且在閱讀時應用用戶時區...