2011-08-21 114 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