Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从T-SQL将datepart转换为LINQ_Linq_Tsql - Fatal编程技术网

从T-SQL将datepart转换为LINQ

从T-SQL将datepart转换为LINQ,linq,tsql,Linq,Tsql,我正在尝试获取从上周到2周前从周日到周六的日期范围 所以今天是2012年10月24日,日期范围是:2012年10月21日-2012年10月27日 我正在尝试获取上周的日期范围,即:2012年10月14日-2012年10月20日 还有两周前的日期范围,即:2012年7月10日至2012年13月10日 我有正确的SQL查询,它是 DECLARE @TodayDayOfWeek INT DECLARE @EndOfPrevWeek DateTime DECLARE @StartOfPrevWeek D

我正在尝试获取从上周到2周前从周日到周六的日期范围 所以今天是2012年10月24日,日期范围是:2012年10月21日-2012年10月27日

我正在尝试获取上周的日期范围,即:2012年10月14日-2012年10月20日
还有两周前的日期范围,即:2012年7月10日至2012年13月10日

我有正确的SQL查询,它是

DECLARE @TodayDayOfWeek INT
DECLARE @EndOfPrevWeek DateTime
DECLARE @StartOfPrevWeek DateTime
DECLARE @EndOf2WeeksAgo DateTime
DECLARE @Start2WeeksAgo DateTime


SET @TodayDayOfWeek = datepart(dw, GetDate())
--get the last day of the previous week (last Sunday)
SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
--get the first day of the previous week (the Monday before last)
SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
SET @EndOf2WeeksAgo = DATEADD(dd, -(@TodayDayOfWeek+7), GetDate())
SET @Start2WeeksAgo = DATEADD(dd, -(@TodayDayOfWeek+13), GetDate())

Select  @StartOfPrevWeek as [Last week start date], @EndOfPrevWeek as [Last Week start date],
@Start2WeeksAgo as [2 Weeks Ago Start], @EndOf2WeeksAgo as [2 Weeks Ago End]    
这导致

[Last week start date] [Last week start date]  [2 Weeks Ago Start] [2 Weeks Ago End] 
10/14/2012             10/20/2012              10/07/2012          10/13/2012
如何将其转换为Linq?我有一个日期列,需要显示这两个日期范围之间的日期,如

last week date   2 weeks ago
10/15/2012        10/08/2012
10/18/2012        10/11/2012

下面是我编写的一个简单的控制台应用程序,它向您展示了完成任务的一种方法。只要根据你的需要调整它

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Test> testData = new List<Test>() {
                new Test() { Id = 1, Date = DateTime.Now.AddDays(-9) },
                new Test() { Id = 2, Date = DateTime.Now.AddDays(-8) },
                new Test() { Id = 3, Date = DateTime.Now.AddDays(-16) },
                new Test() { Id = 4, Date = DateTime.Now.AddDays(-14) },
                new Test() { Id = 5, Date = DateTime.Now.AddDays(-15) },
                new Test() { Id = 6, Date = DateTime.Now.AddDays(-10) },
                new Test() { Id = 7, Date = DateTime.Now.AddDays(-7) }
            };

              DateTime date = DateTime.Now; // 10/24/2012

              DateTime startOneWeekAgo =
                  // prev Sunday 10/14/2012 00:00
                  date.AddDays(-7).Date.AddDays(-(int)date.DayOfWeek),

                  // next Sunday 10/21/2012 00:00
                  endOneWeekAgo = startOneWeekAgo.AddDays(7);

              DateTime startTwoWeeksAgo =
                  // prev Sunday 10/07/2012 00:00
                  startOneWeekAgo.AddDays(-7),

                  // next Sunday 10/14/2012 00:00
                  endTwoWeeksAgo = endOneWeekAgo.AddDays(-7);

            var qryOneWeekAgo =
                            from record in testData
                            where record.Date >= startOneWeekAgo // include start
                            && record.Date < endOneWeekAgo // exclude end
                            select record;

            var qryTwoWeeksAgo =
                             from record in testData
                             where record.Date >= startTwoWeeksAgo // include start
                             && record.Date < endTwoWeeksAgo // exclude end
                             select record;

            Console.WriteLine("------- Dates from 1 Week Ago -------");
            foreach (var record in qryOneWeekAgo)
            {
                Console.WriteLine(
                    string.Format("{0} => {1}", record.Id, record.Date));
            }

            Console.WriteLine();

            Console.WriteLine("------- Dates from 2 Weeks Ago -------");
            foreach (var record in qryTwoWeeksAgo)
            {
                Console.WriteLine(
                    string.Format("{0} => {1}", record.Id, record.Date));
            }

            Console.ReadKey();

        }         
    }

    public class Test
    {
        public int Id { get; set; }
        public DateTime Date { get; set; }
    }

}
------- Dates from 1 Week Ago -------
1 => 10/15/2012 12:52:38 PM
2 => 10/16/2012 12:52:38 PM
6 => 10/14/2012 12:52:38 PM
7 => 10/17/2012 12:52:38 PM

------- Dates from 2 Weeks Ago -------
3 => 10/8/2012 12:52:38 PM
4 => 10/10/2012 12:52:38 PM
5 => 10/9/2012 12:52:38 PM