【问题标题】:Both ISNULL and YEAR SQL functions in in LinqLinq 中的 ISNULL 和 YEAR SQL 函数
【发布时间】:2014-01-30 18:26:09
【问题描述】:

我如何在 Linq 中做到这一点?

SELECT [...] WHERE A.Year = YEAR(ISNULL(B.Date, '1900-01-01'))

字段的数据类型:

A.Year : int not null
B.Date : datetime null

【问题讨论】:

    标签: sql linq tsql


    【解决方案1】:

    LINQ to 实体:

    (...)
    where a.year == SqlFunctions.DatePart("y", b.date ?? new DateTime(1900, 1, 1))
    

    LINQ to SQL:

    (...)
    where a.year == (b.date ?? new DateTime(1900, 1, 1)).Year
    

    【讨论】:

      【解决方案2】:

      不知道您的数据结构在 .NET 方面是什么样的......

      var defaultDate = new DateTime(1900, 1, 1);
      
      var result = from x in xxx
                   where x.Year == (b.Date ?? defaultDate).Year
      

      【讨论】:

        【解决方案3】:
        .Where( f => f.Year == (B.Date ?? new DateTime(1990, 1,1)).Year)
        

        【讨论】:

          猜你喜欢
          • 2017-06-03
          • 2010-09-09
          • 2010-09-29
          • 1970-01-01
          • 1970-01-01
          • 2013-09-23
          • 1970-01-01
          • 1970-01-01
          • 2013-08-07
          相关资源
          最近更新 更多