2011-08-21 91 views
0

我有以下查詢哪些工作正常。但是,它不適用於需要的聯接查詢。LINQ to Entities條件where子句

var ra = from c in _context.Wxlogs 
     select c; 

if (year == "2011") 
{ 
    ra = (IQueryable<Wxlog>)(from c in _context.Wxlogs 
          where c.LogYear == year 
            && (SqlFunctions.DatePart("Month", c.LogDate2) == m3) 
            && c.LogTime.Contains("23:59") 
          orderby c.LogDate2 
          let LogDate = c.LogDate2 
          select new { 
           LogDate, 
           c.Rain_today 
          }); 
} 
else if (year != "2011") 
{ 
    ra = (IQueryable<Wxlog>)(from c in _context.Wxlogs 
          where c.LogYear == year 
          && c.LogMonth == mm 
          && c.LogTime.Contains("08:59") 
          orderby c.LogDate2 
          let LogDate = EntityFunctions.AddDays(c.LogDate2, -1) 
          select new { 
           LogDate, 
           c.Rain_today 
          }); 
} 

因此,我一直在試圖嵌入else條件(something like this answer by Whaheed)沒有任何運氣。

任何幫助,將不勝感激。

+3

不工作怎麼樣?不會給你你期望的數據,拋出錯誤,或? – Tim

+0

連接出現此錯誤:「連接子句中某個表達式的類型不正確,在對」加入「的調用中,類型推斷失敗。 – Corretto

回答

4

可以使用var與條件運營商:

var query = year == "2011" ? 
       from c in _context.Wxlogs 
       where c.LogYear == year 
       && (SqlFunctions.DatePart("Month", c.LogDate2) == m3) 
       && c.LogTime.Contains("23:59") 
       orderby c.LogDate2 
       let LogDate = c.LogDate2 
       select new { 
        LogDate, 
        c.Rain_today 
       }); 
// Second part of conditional 
       : from c in _context.Wxlogs 
       where c.LogYear == year 
       && c.LogMonth == mm 
       && c.LogTime.Contains("08:59") 
       orderby c.LogDate2 
       let LogDate = EntityFunctions.AddDays(c.LogDate2, -1) 
       select new { 
        LogDate, 
        c.Rain_today 
       }); 

這不是主意l,但因爲你是根據年份更改「LogDate」位,它可能是最簡單的方法 - 兩個查詢在太多地方不同,以正常方式提取。 (這不是就像你實際上只是得到了有條件的「where」子句作爲標題所暗示的)。

你需要var這裏是因爲你投射到一個匿名類型的原因。如果你不這樣做,你可以使用if/else塊。 仍然可以通過這樣做,但這有點痛苦。

+0

非常感謝。得到了第一部分工作與格式化。非常感激。總是爭取得到一個合適的標題:-) – Corretto

1

您不能投new { LogDate, c.Rain_today }Wxlog您需要從兩個查詢返回select c

如果你想最後else類型後選擇以下

var ra2 = from r in ra 
      select new { 
       LogDate, 
       c.Rain_today 
      }; 
+0

謝謝,但不能得到那個工作。 – Corretto

1

嘗試(評論後EDIT)只是一部分,你可以:

if (year == "2011") 
{ 
ra = (from c in _context.Wxlogs 
     where c.LogYear == year && 
      && (SqlFunctions.DatePart("Month", c.LogDate2) == m3) 
      && c.LogTime.Contains("23:59") 
      orderby c.LogDate2 
      let LogDate = EntityFunctions.AddDays(c.LogDate2, year == "2011" ? 0 : -1) 
      select new { 
         LogDate, 
         c.Rain_today 
         }).AsQueryable(); 
} 
else if (year != "2011") 
{ 
ra = (from c in _context.Wxlogs 
     where c.LogYear == year && 
      && c.LogMonth == mm 
      && c.LogTime.Contains("08:59") 
      orderby c.LogDate2 
      let LogDate = EntityFunctions.AddDays(c.LogDate2, year == "2011" ? 0 : -1) 
      select new { 
         LogDate, 
         c.Rain_today 
         }).AsQueryable(); 
} 
+0

感謝您的支持,但我在Where子句 – Corretto

+0

上得到一個模糊的innvocation錯誤請檢查我的EDIT ... – Yahia

+0

謝謝Yahia,但Jon的答案上面的工作非常好。 – Corretto