Linq中的ISNULL和YEAR SQL都在函数中

时间:2014-01-30 18:26:09

标签: sql linq tsql

我如何在Linq中执行此操作?

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

字段的数据类型:

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

3 个答案:

答案 0 :(得分:1)

LINQ to Entities:

(...)
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

答案 1 :(得分:1)

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

var defaultDate = new DateTime(1900, 1, 1);

var result = from x in xxx
             where x.Year == (b.Date ?? defaultDate).Year

答案 2 :(得分:1)

.Where( f => f.Year == (B.Date ?? new DateTime(1990, 1,1)).Year)