Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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
Linq to SQL:获取连续的每日视图数_Sql_Linq To Sql - Fatal编程技术网

Linq to SQL:获取连续的每日视图数

Linq to SQL:获取连续的每日视图数,sql,linq-to-sql,Sql,Linq To Sql,我有一个记录用户何时查看页面的表。该表如下所示: ID | Date 1 | 01/01/10 00:00:01.000 2 | 03/01/10 00:00:01.000 3 | 03/01/10 00:00:02.000 4 | 04/01/10 00:00:01.000 5 | 05/01/10 00:00:01.000 6 | 05/01/10 00:00:02.000 7 | 05/01/10 00:00:03.000 使用LINQtoSQL,我想计算一个页面最后一组连

我有一个记录用户何时查看页面的表。该表如下所示:

ID | Date
1  | 01/01/10 00:00:01.000
2  | 03/01/10 00:00:01.000
3  | 03/01/10 00:00:02.000
4  | 04/01/10 00:00:01.000
5  | 05/01/10 00:00:01.000
6  | 05/01/10 00:00:02.000
7  | 05/01/10 00:00:03.000
使用LINQtoSQL,我想计算一个页面最后一组连续浏览的天数,但当然我不知道该怎么做。例如,对上面的数据运行查询应该返回3


非常感谢您的帮助:)

这是测试数据。我在列表的末尾添加了10月6日,以确保结果只拾取最后一个连续的日期集

List<DateTime> viewDates = new List<DateTime>();
viewDates.Add(new DateTime(10, 10, 1));
viewDates.Add(new DateTime(10, 10, 3));
viewDates.Add(new DateTime(10, 10, 3));
viewDates.Add(new DateTime(10, 10, 4));
viewDates.Add(new DateTime(10, 10, 5));
viewDates.Add(new DateTime(10, 10, 5));
viewDates.Add(new DateTime(10, 10, 5));
viewDates.Add(new DateTime(10, 10, 6));


这是针对
LINQ
的实现,可读性较差:

var consecutiveDates = viewDates
    .Where(viewDate => viewDate <= lastConsecutiveDate)
    .Distinct()
    .OrderByDescending(viewDate => viewDate.Date)
    .Aggregate
    (
        new { List = new List<DateTime>() },
        (result, viewDate) =>
        {
            int count = result.List.Count;
            if ((count > 0) && (result.List[count - 1].Date.AddDays(-1) != viewDate.Date))
            {
                return new { result.List };
            }

            result.List.Add(viewDate);
            return new { result.List };
        },
        a => a.List
    );
var consutivedates=viewDates
.Where(viewDate=>viewDate viewDate.Date)
总数的
(
新建{List=new List()},
(结果,查看日期)=>
{
int count=result.List.count;
如果((计数>0)和&(result.List[count-1].Date.AddDays(-1)!=viewDate.Date))
{
返回新的{result.List};
}
结果.列表.添加(查看日期);
返回新的{result.List};
},
a=>a.列表
);
Console.WriteLine(continuedates.Count())将打印出“3”。

我希望对我的代码进行更多的重构,但我休息了。

你不想返回7而不是3吗?我显然是在连续天数本身,而不是连续天数上的记录数。啊,是的-很抱歉我误解了你的问题。此查询与连续天数无关,它只返回一天的浏览量。哎哟。。我误解了asker的问题。我已经编辑了我的代码。(-2是痛苦的…)我理解你的痛苦,我很乐意改变我的投票,但你又误解了这个问题。正如OP在评论中所说,他关注的是连续几天,而不是最近一天的浏览量。它应该是类似于distinct、revert,然后计算连续的天数。。。我不知道如何使用Linq,但您的解决方案仍处于关闭状态。
var consecutiveDates = new List<DateTime>();
foreach (var item in viewDates.Where(viewDate => viewDate <= lastConsecutiveDate).Distinct().OrderByDescending(a => a.Date))
{

    //break if datediff is not 1
    int count = consecutiveDates.Count;
    if ((count > 0) && (consecutiveDates[count - 1].Date.AddDays(-1) != item.Date))
    {
        break;
    }

    consecutiveDates.Add(item);
}
var consecutiveDates = viewDates
    .Where(viewDate => viewDate <= lastConsecutiveDate)
    .Distinct()
    .OrderByDescending(viewDate => viewDate.Date)
    .Aggregate
    (
        new { List = new List<DateTime>() },
        (result, viewDate) =>
        {
            int count = result.List.Count;
            if ((count > 0) && (result.List[count - 1].Date.AddDays(-1) != viewDate.Date))
            {
                return new { result.List };
            }

            result.List.Add(viewDate);
            return new { result.List };
        },
        a => a.List
    );