【问题标题】:ASP.Net LINQ to SQL group by syntax for System.Generic.IEnumerable CollectionASP.Net LINQ to SQL 按 System.Generic.IEnumerable 集合的语法分组
【发布时间】:2011-11-29 20:40:14
【问题描述】:

.Net 应用程序调用存储过程并返回存储为“结果”的集合。我希望能够按“CreatorLineOfBusinessID”对结果进行分组。它返回该字段以及其他几个字段。我想对“result”数据集使用 LINQ 查询。提前致谢。

Dim result As IEnumerable(Of eRefer_Reports.uspReport_ReferralsSentBetweenLinesOfBusinessResult)

             strFromLOBID = " "
            Session("FromLineOfBusiness") = strFromLOBID
            strToLOBID = " "
            Session("ToLineOfBusiness") = strToLOBID

            result = repository.GetQueryResults(CDate(Me.txtStartDate.Text), CDate(Me.txtEndDate.Text), strFromLOBID, strToLOBID)
            BindGridView(result)          

【问题讨论】:

  • 正如Lcarus 所指出的,您不需要使用 LINQ 来进行此分组。因为您的结果已经是IEnumerable<uspReport_ReferralsSentBetweenLinesOfBusinessResult>,您也可以使用 GroupBy 方法。
  • @Susan 你过得怎么样?您能否将报告分组?
  • 抱歉,已经有一段时间了,但一直在度假。我在语法上苦苦挣扎。我正在尝试:BindGridView(result.GroupBy(CreatorLineOfBusinessID)),但这不起作用。对不起,我是个新手!

标签: asp.net linq-to-sql group-by


【解决方案1】:

你对 Linq 分组做过研究吗?在此处尝试 101 个示例:http://msdn.microsoft.com/en-us/vstudio/bb688088

Dim orderGroups = From p In results Group p By Key = p.CreatorLineOfBusinessID Into Group _
Select CreatorBusinessId = Key, Referrals = Group

然后使用 Key 和属性 Referrals 迭代 orderGroups,这将是一个 IEnumerable<eRefer_Reports.uspReport_ReferralsSentBetweenLinesOfBusinessResult>

For Each lobBusiness in orderGroups

 ' Get the lobBusinessId : .CreatorBusinessId
 ' Get the IEnumerable results: .Referrals

Next

【讨论】:

    【解决方案2】:
    Dim query = result.GroupBy(Function(x) x.ID).ToList()
    

    应该按照你想要的方式对结果进行分组。

    【讨论】:

    • 不是很挑剔,但她确实说过“LINQ 查询”。 :D 否则我也会使用这个Enumerable.GroupBy(Of TSource, TKey)
    • @JeremyChild:是的,你是对的。在这种情况下,Jeremy 的答案就是她想要的。
    • @Lcarus 但它是同一件事 :D LINQ To Objects 无疑会使用 IEnumerable.GroupBy 而你没有意识到这一点。使用 GroupBy 方法可以使代码更简洁。
    • Dim xyz = result.GroupBy(Function(x) x.CreatorLineOfBusinessName) BindGridView(xyz) BindGridView 命令给我一个错误,说结果无法转换为 Systems.Collections.Generic.IEnumerable( eRefer_Reports.usp_ReferralsSentBetweenBusinessLinesResult 接口 IEnunerable(Of OUT T) 中的 'out' 泛型参数 'T' 的要求
    • @Susan:尝试在最后调用 ToList();应该返回 List(Of usp_ReferralsSentBetweenBusinessLinesResult) 并且您应该能够绑定到您的 gridview
    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多