c# - 旋转 IEnumerable 列表

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

我正在尝试将一些 groupby/crosstabbing 逻辑应用于用户定义对象的 IEnumerable 列表,并且想知道是否有人可以帮助我。我一直坚持使用现有的(相当烦人的)对象模型,但无论如何,这里......

考虑下面的类,我将其压缩为仅相关属性,以便您了解要点...

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

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

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

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

我不确定这对于 IEnumerable List 是否可行,我已经对 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();
}

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

提前致谢。

最佳答案

我正在使用 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()
                            })
    })




我假设你的类(class)看起来像这样

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;}
}

关于c# - 旋转 IEnumerable 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9360555/

相关文章:

c# - 调用Windows API时CLR怎么比我快

c# - 使用BoundField.DataFormatString格式化货币数据,无$'s and negative numbers should be in ( )'s

sql - Linq 到 SQL : How do I stop the auto generated object name from being renamed?

c# - LINQ to SQL (CE) 速度与 SqlCe

C# - 在一个类中从一个方法继承到另一个方法?

c# - 推迟 RadioButton 更改

c# - 从程序集 app.config 文件中读取 Web 服务信息

javascript - 启用/禁用 <asp :Panel> and all its control using Javascript

c# - ASP.net 4.0 c# 中的可选参数

linq - 用 LINQ-to-SQL 模仿 SQL 插入触发器