强类型视图投影 linq

发布于 2024-10-06 17:03:13 字数 640 浏览 5 评论 0原文

我花了几个小时试图弄清楚如何将从 linq 投影到强类型视图的数据整形。我的问题是我认为我的问题是我不确定如何使用 IEnumberable 和 IGrouping。

这是 linq:

var spec = from a in _entities.Approvals
                   join b in _entities.ApprovalSpecifications on a.HeaderlID equals b.HeaderlID into g
                   where a.ApprovalID == id
                   group a by a.HeaderlID into groupedByHeader
                   select new 
                   {
                       Key = groupedByHeader.Key,
                       groupedByHeader
                   };

任何人都可以建议我应该采用的方法来解决这个问题吗?我认为为此创建一个类效果最好,但正如我所提到的,我不确定如何使用 IGrouping 来构建一个类。任何帮助表示赞赏!

I have spent hours upon hours trying to figure out how to shape the data projected from linq to a strongly type view. My problem is I think my problem is I am unsure how to use IEnumberable and IGrouping.

Here is the linq:

var spec = from a in _entities.Approvals
                   join b in _entities.ApprovalSpecifications on a.HeaderlID equals b.HeaderlID into g
                   where a.ApprovalID == id
                   group a by a.HeaderlID into groupedByHeader
                   select new 
                   {
                       Key = groupedByHeader.Key,
                       groupedByHeader
                   };

Can anyone suggest the method with which I should approach this? I am thinking a class for this would work best, but as I mentioned I'm not sure how to use IGrouping to build a class. Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-10-13 17:03:13

像这样的东西吗?

class StronglyTypedGrouping {
  public object Key { get; set; } // I can't infer Key type from the snippet.
  public IEnumerable<Approval> Approvals { get; set; }
}


var spec = from a in _entities.Approvals
           join b in _entities.ApprovalSpecifications on 
             a.HeaderlID equals b.HeaderlID into g
           where a.ApprovalID == id
           group a by a.HeaderlID into groupedByHeader
           select new StronglyTypedGrouping {
               Key = groupedByHeader.Key,
               Approvals = groupedByHeader
           };

Something like this?

class StronglyTypedGrouping {
  public object Key { get; set; } // I can't infer Key type from the snippet.
  public IEnumerable<Approval> Approvals { get; set; }
}


var spec = from a in _entities.Approvals
           join b in _entities.ApprovalSpecifications on 
             a.HeaderlID equals b.HeaderlID into g
           where a.ApprovalID == id
           group a by a.HeaderlID into groupedByHeader
           select new StronglyTypedGrouping {
               Key = groupedByHeader.Key,
               Approvals = groupedByHeader
           };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文