Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
C# 对没有分组值的记录进行分组 请考虑以下记录: Id Week Value ----------------------------- 1 1 1000 2 1 1200 3 2 800 4 3 1800 5 3 1100 6 3 1000_C#_Linq - Fatal编程技术网

C# 对没有分组值的记录进行分组 请考虑以下记录: Id Week Value ----------------------------- 1 1 1000 2 1 1200 3 2 800 4 3 1800 5 3 1100 6 3 1000

C# 对没有分组值的记录进行分组 请考虑以下记录: Id Week Value ----------------------------- 1 1 1000 2 1 1200 3 2 800 4 3 1800 5 3 1100 6 3 1000,c#,linq,C#,Linq,我想将记录分组4周,但第4周没有记录。例如: Week Count --------------------- 1 2 2 1 3 3 4 0 我怎样才能和linq一起做到这一点 谢谢首先,您需要一个星期数组,然后此查询可能会有所帮助 var weeks = new List<int>{1,2,3,4} var q = from w in wee

我想将记录分组4周,但第4周没有记录。例如:

Week         Count
---------------------
1              2
2              1
3              3
4              0
我怎样才能和linq一起做到这一点


谢谢

首先,您需要一个星期数组,然后此查询可能会有所帮助

    var weeks = new List<int>{1,2,3,4}

    var q = from w in weeks
        join rw in (
            from r in table
            group r by r.Week into g
            select new {week = g.Key, count = g.Count()}) on w  equals rw.week into p
        from x2 in p.DefaultIfEmpty()
        select new {w, count = (x2 != null ? x2.count : 0)};
你可以试试

    var result = Enumerable.Range(1, 4)
            .GroupJoin(table,
                week => week,
                record => record.Week,
                (week, records) => new { Week = week, Count = records.Count() });
正如jessehouwing所建议的那样,Enumerable.Range将返回可能用作联接中左外键的周数

然后,GroupJoin将接受作为参数 返回左外键的lambda/delegate/方法 从表中提取正确键的lambda/delegate/method。 生成结果项的lambda/delegate/method。 问候,,
Daniele.

你不能有EF和Linq2SQL。选择一个。构建一个视图,以便在SQL Server中为您执行此操作,然后使用EF查询视图。您可能可以使用Enumerable.Range1,4指定您要查找的确切数字…然后在SQL中进行计数并在代码中进行分组。创建一个只包含1-4范围内的数字的集合,并将其与包含左连接数据的集合合并。请尝试遵循有关实现的MSDN文档。这不会编译even@Kerezo哈赫什·米科南;