Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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

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
C# 使用linq使用组嵌套选择_C#_Linq_Select_Group By - Fatal编程技术网

C# 使用linq使用组嵌套选择

C# 使用linq使用组嵌套选择,c#,linq,select,group-by,C#,Linq,Select,Group By,我正在尝试将以下查询转换为linQ表达式: SELECT t1.lastchanged, currentstatus, (SELECT count(ID) FROM [Issues] t2 WHERE t2.CurrentStatus = t1.currentstatus and t2.lastchanged <= t1.lastchanged) AS running_total FROM [Issues] t1 GROUP BY t1.lastch

我正在尝试将以下查询转换为linQ表达式:

SELECT t1.lastchanged, 
    currentstatus,
    (SELECT count(ID) 
    FROM [Issues] t2 
    WHERE t2.CurrentStatus = t1.currentstatus and t2.lastchanged <= t1.lastchanged) AS running_total
FROM [Issues] t1 
GROUP BY t1.lastchanged, currentstatus
ORDER BY t1.lastchanged

有什么建议吗?

为了让嵌套的select像在sql中一样,从
模型
集合中再次选择

var statusCounts = models.GroupBy(x => new { x.CurrentStatus, x.LastChanged })
                         .Select(g => new 
                         { 
                             CurrentStatus = g.Key.CurrentStatus, 
                             LastChanged = g.Key.LastChanged,
                             Count = models.Count(m => m.CurrentStatus == g.Key.CurrentStatus &&
                                                       m.LastChanged  <= g.Key.LastChanged)
                         })
                         .OrderBy(item => item.Key.LastChanged);
var statusCounts=models.GroupBy(x=>new{x.CurrentStatus,x.LastChanged})
.选择(g=>new
{ 
CurrentStatus=g.Key.CurrentStatus,
LastChanged=g.Key.LastChanged,
Count=models.Count(m=>m.CurrentStatus==g.Key.CurrentStatus&&
m、 LastChanged项。键。LastChanged);
或在查询语法中:

var statusCounts = from t1 in models
                   group t1 by new { x.CurrentStatus, x.LastChanged } into g
                   orderby g.Key.LastChanged
                   select new 
                   { 
                       CurrentStatus = g.Key.CurrentStatus, 
                       LastChanged = g.Key.LastChanged,
                       Count = models.Count(m => m.CurrentStatus == g.Key.CurrentStatus &&
                                                 m.LastChanged  <= g.Key.LastChanged)
                   };
var statusCounts=来自模型中的t1
将t1按新的{x.CurrentStatus,x.LastChanged}分组为g
orderby g.Key.LastChanged
选择新的
{ 
CurrentStatus=g.Key.CurrentStatus,
LastChanged=g.Key.LastChanged,
Count=models.Count(m=>m.CurrentStatus==g.Key.CurrentStatus&&

m、 太好了!这就是我需要的。谢谢
var statusCounts = from t1 in models
                   group t1 by new { x.CurrentStatus, x.LastChanged } into g
                   orderby g.Key.LastChanged
                   select new 
                   { 
                       CurrentStatus = g.Key.CurrentStatus, 
                       LastChanged = g.Key.LastChanged,
                       Count = models.Count(m => m.CurrentStatus == g.Key.CurrentStatus &&
                                                 m.LastChanged  <= g.Key.LastChanged)
                   };