首页 > 解决方案 > Linq 中的 Sql YEAR(),MONTH()

问题描述

我一直在寻找几天来解决 linq 上的 sql 查询。这是我的 sql 查询([Date] 的格式为 datatime2):

SELECT [Date], TestingValues 
FROM [SalesValue].[dbo].[TestTable] 
WHERE CONCAT(DATEPART(yyyy,[Date]),DATEPART(MM,[Date])) = '201801'

我的查询看起来像这样,我无法继续。我不知道如何从 b_TestTable.Date 中获取一年零一个月:

string dat = DateTime.ParseExact(dats, "yyyy-MM-dd", CultureInfo.CurrentCulture).ToString("yyyyMM");

var testQuery = (
        from b_TestTable in repos.GetTable<TestTable>() 
        where b_TestTable.Date == dat 
        select b_TestTable.TestingValues)
    .ToArray();

谁能帮我查询在 linq 中的样子?感谢您的帮助。

标签: c#.netlinq

解决方案


您可以使用

var dat = DateTime.ParseExact(dats, "yyyy-MM-dd", CultureInfo.CurrentCulture);

var testQuery = (
        from b_TestTable in repos.GetTable<TestTable>() 
        where b_TestTable.Date.Year == dats.Year 
            && b_TestTable.Date.Month == dats.Month 
        select b_TestTable.TestingValues)
    .ToArray();

推荐阅读