【问题标题】:Grouping using LINQ [duplicate]使用 LINQ 进行分组
【发布时间】:2011-01-13 14:46:21
【问题描述】:

将简单的 SQL 查询转换为 LINQ 查询(使用 vb btw)

这是我的 SQL:

SELECT     USRDEFND5
FROM         int_gp_employee
GROUP BY USRDEFND5

xml 看起来像这样:

<int_gp_employee>
  <row>
    ....
    <usrdefnd5>Some GUID</usrdefnd5>
  </row>
</int_gp_employee>

我尝试了许多不同的 LINQ 变体。我目前的说法是:

From b In xmlFile...&lt;row&gt; Group b...&lt;usrdefnd5&gt; By b...&lt;usrdefnd5&gt; INTO group

当我遍历结果集合时,每一行 (17000) 都会显示出来。

感谢观看。

【问题讨论】:

    标签: .net sql xml vb.net linq


    【解决方案1】:

    恐怕我不确定 VB 的等价物,但在 C# 中应该是:

    var query = from row in xmlFile.Descendants("row")
                group row by (string) row.Element("usrdefnd5");
    

    不使用 XML 文字,VB 将是:

    Dim query = From row In document.Descendants("row") _
                Group row By CStr(row.Element("usrdefnd5"))
    

    编辑:如果您只需要不同的键,那么类似:

    Dim query = From row In document.Descendants("row") _
                Select CStr(row.Element("usrdefnd5")) _
                Distinct
    

    【讨论】:

    • 仍然返回所有这些。我正在寻找 usrdefnd5 的不同值。
    • 它应该返回所有行,但按该字段分组。每个组将是具有该键的行序列。您需要行,还是只需要键?
    • 我应该说,我只需要 DISTINCT 键。
    • 我不知道为什么这不起作用:[code] Dim xmlFile As XDocument = XDocument.Load("h:\test\xml\int_gp_employee.xml") dim branch_ids = From row IN xmlFile... SELECT row... 在 branch_ids console.writeline(b.value) next [/code] 中的每个 b 都不同 [/code] 抱歉,我还不知道如何标记 cmets 部分.
    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 2011-06-20
    • 2013-01-23
    • 2016-08-08
    • 2013-04-11
    • 2016-04-02
    • 2014-01-06
    • 2016-01-16
    相关资源
    最近更新 更多