【问题标题】:Pivoting an IEnumerable List透视 IEnumerable 列表
【发布时间】:2012-02-20 11:41:49
【问题描述】:

我正在尝试将一些 groupby/crosstabbing 逻辑应用于用户定义对象的 IEnumerable 列表,并且想知道是否有人可以帮助我。我被一个现有的(相当烦人的)对象模型困住了,但不管怎样……

请考虑以下类,我将仅将其浓缩为相关属性,以便您了解 jist...

public class Holding
{
   private void Institution;
   private string Designation;
   private Owner Owner;
   private Event Event;
   private Shares Shares;
}

我想将其转换为满足以下条件的列表...

  • 对象按机构分组。
  • 该机构的父列表包含一个新对象列表,该对象具有唯一的名称和所有者组合。
  • 现在,对于每一个指定和所有者的组合,我们都会获得另一个独特事件的子列表。

所以它基本上是 3 个深度列表。

我不确定这是否可以通过 IEnumerable 列表实现,到目前为止,我已经玩弄了很多 GroupBy 扩展方法,但无济于事。我最想这样做,但我正在使用 linq-to-sql 来获取如下的初始持股清单,这可能是开展业务的更好场所......

public static List<Holding> GetHoldingsByEvents(
    int eventId1,
    int eventId2)
{
    DataClassesDataContext db = new DataClassesDataContext();

    var q = from h in db.Holdings
             where
               h.EventId == eventId1 ||
               h.EventId == eventId2
             select h;

    return q.Distinct().ToList();
}

任何帮助/指导将不胜感激...

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    我正在使用 ToLookup 方法,这是一种分组,它有两个参数,第一个是用于定义组键的函数,下一个是用作选择器的函数(从匹配中获取什么)。

    items.ToLookup(c=>c.Institution.InstitutionId, c => new {c.Designation, c.Owner, c.Event})
        .Select(c => new {
            // find the institution using the original Holding list
            Institution = items.First(i=>i.Institution.InstitutionId == c.Key).Institution,
            // create a new property which will hold the groupings by Designation and Onwner
            DesignationOwner = 
                    // group (Designation, Owner, Event) of each Institution by Designation and Owner; Select Event as the grouping result
                    c.ToLookup(_do => new {_do.Designation, _do.Owner.OwnerId}, _do => _do.Event)
                                .Select(e => new {
                                    // create a new Property Designation, from e.Key
                                    Designation = e.Key.Designation,
                                    // find the Owner from the upper group ( you can use items as well, just be carreful this is about object and will get the first match in list)
                                    Owner = c.First(o => o.Owner.OwnerId == e.Key.OwnerId).Owner,
                                    // select the grouped events // want Distinct? call Distinct
                                    Events = e.Select(ev=>ev)//.Distinct()
                                })
        })
    




    我假设你的课程看起来像这样

    public class Holding
    {
       public Institution Institution {get; set;}
       public string Designation {get; set;}
       public Owner Owner {get; set;}
       public Event Event {get; set;}   
    }
    public class Owner 
    {
      public int OwnerId {get; set;}
    }
    
    public class Event 
    {
      public int EventId {get; set;}
    }
    
    public class Institution 
    {
      public int InstitutionId {get; set;}
    }
    

    【讨论】:

    • 你,我的好人,是冠军和学者。我现在在电脑前为你唱贝蒂·米德勒的“我翅膀下的风”。我完全不知道 linqs ToLookup... TY!
    • 欢迎,这真的很好!
    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多