【问题标题】:GroupBy in LINQ return me invalid values using Entity Framework 4.0LINQ 中的 GroupBy 使用 Entity Framework 4.0 返回无效值
【发布时间】:2015-01-22 01:05:44
【问题描述】:

我在我的应用程序中使用EntityFramework 4.0。我有一个以int 作为参数的存储过程。我已使用 Entity Framework 在我的应用程序中将此 proc 作为函数导入导入。这个过程返回给我两列NameNumber。我正在使用 LINQ 查询对这个结果进行分组

var availName = dataContext.sp_GetAvailable(1).ToList().GroupBy(x => x.TXT_NAME).Select(x => new { Name= x.Key, Communication = string.Join(",", x) });

它将Name 分组,但值返回为过程名称,即。 Communication = string.Join(",", x) 通信返回我 sp_GetAvailable 作为值。

我错过了什么??

【问题讨论】:

  • 我怀疑类似string.Join(",", x.Select(xi => xi.Number))

标签: c# asp.net linq linq-to-sql entity-framework-4


【解决方案1】:

GroupBy 返回 IEnumerable 其中 IGrouping 有 x.Key 类型的密钥 和 IEnumerable 类型的值,其中 T 是 x 的类型(不是字符串)

要得到你想要的,你应该使用:

var availName = dataContext.sp_GetAvailable(1).ToList().GroupBy(x => x.TXT_NAME).Select(x => new { Name= x.Key, Communication = string.Join(",", x.Select(c=>c.Number)) })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多