1

id'喜欢计算用户编辑或创建的文档数量。因此我有一个包含信息的数据表。像这样:

Input
DocumentName | ModifiedBy | CreatedBy
 a              Frank         Frank
 b              Mike          Frank
 c              John          Mike

那应该是输出:

Name   DocumentsModified(Total)  DocumentsCreated(Total)
Frank      1                        2
Mike       1                        1
John       1                        0

因此,计算一个人编辑的所有文档的方法如下

var query = from queryResult in resultTable.AsEnumerable()
group queryResult by queryResult.Field<string>("ModifiedBy") into rowGroup
select new 
{
    Name = rowGroup.Key, ModifiedDocuments = rowGroup.Count()
};

这工作正常。现在我还必须计算“Creator”列的值。这可能吗?如果可以,我该怎么做?好的,我可以遍历表,但最好在 LinQ 查询中完成此操作。

我尝试使用“group queryResult by new {}”,但我不确定我是否走在正确的轨道上。

4

2 回答 2

1

你不能用同样的方法CreatedBy吗?

var query2 = from queryResult in resultTable.AsEnumerable()
group queryResult by queryResult.Field<string>("CreatedBy") into rowGroup
select new 
{
    Name = rowGroup.Key, ModifiedDocuments = rowGroup.Count()
};

然后使用按名称排序的 2 个查询。

于 2012-06-22T15:19:43.317 回答
1

最好的办法是分别查询创建和修改,然后将结果与连接结合起来。

var modifiedCount = from queryResult in resultTable.AsEnumerable()
group queryResult by queryResult.Field<string>("ModifiedBy") into rowGroup
select new 
{
    Name = rowGroup.Key, ModifiedDocuments = rowGroup.Count()
};

var createdCount = from queryResult in resultTable.AsEnumerable()
group queryResult by queryResult.Field<string>("CreatedBy") into rowGroup
select new 
{
    Name = rowGroup.Key, CreatedDocuments = rowGroup.Count()
};

var finalResult = from first in modifiedCount 
                  join second in createdCount
                  on first.Name equal second.Name
                  select new 
                  {
                     first.Name,
                     first.ModifiedDocuments,
                     second.CreatedDocuments 
                  };
于 2012-06-22T15:27:50.247 回答