我在SQL Server中有这个需要转移到LINQ的查询:
表:
CaseId EventId Activity Timestamp Resource Cost
-----------------------------------------------------------------
1 35654423 register request 2010-12-30 Pete 50
1 35654423 examine thoroughly 2010-12-30 Pete 100
1 35654423 check ticket 2010-12-30 Pete 200
1 35654423 decide 2010-12-30 Pete 200
. . . . . .
. . . . . .
. . . . . .
. . . . . .
And so on...
查询:
DECLARE @tblCantidades TABLE
(
iRow INT,
Activity VARCHAR(50),
Cantidad SMALLINT
);
INSERT INTO @tblCantidades (iRow, Activity, Cantidad)
SELECT
TblLog.iRow,
TblLog.Activity,
COUNT(1) AS Cantidad
FROM
(SELECT
ROW_NUMBER() OVER(PARTITION BY CaseId ORDER BY CaseId ASC) AS iRow,
LibroLog.*
FROM
ExampleLog AS LibroLog) AS TblLog
GROUP BY
TblLog.iRow, Activity
ORDER BY
TblLog.iRow, Cantidad DESC;
此查询返回:
Irow Activity Cantidad
---------------------------------
1 register request 6
2 examine casually 3
2 check ticket 2
2 examine thoroughly 1
3 check ticket 4
3 examine casually 1
3 examine thoroughly 1
4 decide 6
5 reinitiate request 2
5 pay compensation 2
5 reject request 2
6 examine thoroughly 1
6 check ticket 1
. . .
. . .
. . .
. . .
and so on
我有这个Linq,但我需要两个变量(startList和final),因为第一个变量将用于编写.CSV
并且我没有问题。
其中" log"是一个DataTable,它具有来自CSV文件的数据,其字段与SQL Server中的表相同,并且关系是一个Dictionary(字符串,字符串),因为程序需要读取具有相同值但不同字段名称的任何Log,字典值是CSV文件的名称
DataTable tblCantidades = new DataTable();
tblCantidades.Columns.Add("iRow", typeof(int));
tblCantidades.Columns.Add("Activity", typeof(string));
tblCantidades.Columns.Add("Cantidad", typeof(short));
var startList = log.AsEnumerable()
.OrderBy(order => order.Field<string>(relationship["Order"]))
.GroupBy(group => group.Field<string>(relationship["Case"]))
.Select(select => new
{
everything = select,
count = select.Count()
})
.SelectMany(many => many.everything
.Select(select => select)
.Zip(Enumerable.Range(1, many.count), (records, i) => new
{
iRow = i,
records
})).ToList();
var final = from table in startList
group table by new
{
table.iRow,
activity = table.records.Field<string>(relationship["Activity"])
} into collection
select new
{
iRow = collection.Key.iRow,
Activity = collection.Key.activity,
Count = collection.Key.activity.Count()
} into finalCollection
orderby new
{
finalCollection.iRow,
finalCollection.Count
}
select finalCollection;
但是最终变量没有返回任何内容