LINQ groupby和count

时间:2012-06-22 08:51:34

标签: linq count group-by

我有以下LINQ查询:

From metasect In DirectCast(Form.ChildDocBox.Tag, META_DOCUMENT).META_SECTIONS
From metaset In metasect.META_SETS
From metacell In metaset.META_CELLS
From lookup In metacell.CHILD_META_CELL_CELLS_LOOKUPS
Where lookup.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_DOCUMENT.META_DOC_ID = docToDelete.META_DOC_ID
Select metasect.META_SECT_NAME + " gets from " + lookup.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_SECT_NAME Distinct

选择指令选择一个可以重复的表达式,因为在同一对父上有多个查找,这就是我添加 Distinct 子句的原因。

现在,我想计算每个父/子夫妇有多少查找

这样的东西
SELECT metasect.META_SECT_NAME + " gets from " + lookup.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_SECT_NAME,
       COUNT(lookup)
FROM ...
WHERE ...
GROUP BY metasect.META_SECT_NAME + " gets from " + lookup.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_SECT_NAME

提前感谢任何建议:)

1 个答案:

答案 0 :(得分:0)

我用两步法解决了

Dim all = From metasect In DirectCast(Form.ChildDocBox.Tag, META_DOCUMENT).META_SECTIONS
          From metaset In metasect.META_SETS
          From metacell In metaset.META_CELLS
          From lookup In metacell.CHILD_META_CELL_CELLS_LOOKUPS
          Where lookup.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_DOCUMENT.META_DOC_ID = docToDelete.META_DOC_ID
          Select lookup


For Each grp As IGrouping(Of String, ICELLS_LOOKUP) In
         all.GroupBy(Function(l) l.CHILD_META_CELL_META_CELL.META_SET.META_SECTION.META_SECT_NAME + " gets from " + l.PARENT_META_CELL_META_CELL.META_SET.META_SECTION.META_SECT_NAME)

    'do some stuff with grp.Key() & grp.Count()'

Next