【问题标题】:How to get values from IGrouping如何从 IGrouping 获取值
【发布时间】:2012-01-21 04:53:55
【问题描述】:

我对@9​​87654321@ 和Select() 方法有疑问。

假设我有一个 IEnumerable<IGrouping<int, smth>> 以这种方式:

var groups = list.GroupBy(x => x.ID);

其中listList<smth>

现在我需要以某种方式将每个 IGrouping 的值传递给另一个列表:

foreach (var v in structure)
{
    v.ListOfSmth = groups.Select(...); // <- ???
}

谁能建议如何在这种情况下从IGrouping&lt;int, smth&gt; 获取值 (List&lt;smth&gt;)?

【问题讨论】:

  • ID 通常是一个身份字段,应该是唯一的,如果您只是尝试删除重复数据,请尝试使用 Distinct() 代替。如果它是 list.GroupBy(x => x.SubID) 那么使用分组是有意义的,但在这种情况下,您很可能希望保留分组和 foreach(var grp in groups){grp.ToList() ; } 会这样做

标签: c# linq select igrouping


【解决方案1】:

只需这样做:

// this will "split" the list into groups
var groups = list.GroupBy(x => x.ID);

// groups is a "collection of lists"
foreach (var sublist in groups)
{
  // now the sublist is only a part of the original list
  // to get which is the value of ID, you can use sublist.Key
}

你不需要Select().GroupBy(expr) 制作“列表列表”,有点。

【讨论】:

    【解决方案2】:

    由于IGrouping&lt;TKey, TElement&gt; 实现了IEnumerable&lt;TElement&gt;,您可以使用SelectMany 将所有IEnumerables 重新组合为一个IEnumerable

    List<smth> list = new List<smth>();
    IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.id);
    IEnumerable<smth> smths = groups.SelectMany(group => group);
    List<smth> newList = smths.ToList();
    

    这是一个构建/运行的示例:https://dotnetfiddle.net/DyuaaP

    此解决方案的视频评论:https://youtu.be/6BsU1n1KTdo

    【讨论】:

    • 这不是和普通的“orderBy”一样吗? List newList = list .OrderBy(x => x.id).ToList();
    • @MorgoZ, OrderBy(x =&gt; x.id) 将按 ID 升序对它们进行排序。将此与.GroupBy(x =&gt; x.id).SelectMany(group =&gt; group) 进行对比,后者将按照 ID 的首次出现顺序对它们进行排序。如果原始 ID 的顺序是:[1,3,2,2,2,3,0],那么将它们分组然后展平到一个列表中,就会将 ID 放在新的顺序中:[1,3,3, 2,2,2,0]。
    • 它对我不起作用..它说无法通过用法推断类型
    • 无法通过用法推断类型
    • @zcoop98,没有。那是行不通的。这将为您提供每个组中不是所需结果的第一个元素:dotnetfiddle.net/sXwnGM
    【解决方案3】:

    假设你有类似 MyPayments 的类

     public class Mypayment
    {
        public int year { get; set; }
        public string month { get; set; }
        public string price { get; set; }
        public bool ispaid { get; set; }
    }
    

    你有一个 MyPayments 列表

    public List<Mypayment> mypayments { get; set; }
    

    并且您希望按年份对列表进行分组。您可以像这样使用 linq:

    List<List<Mypayment>> mypayments = (from IGrouping<int, Mypayment> item in yearGroup
                                                    let mypayments1 = (from _payment in UserProjects.mypayments
                                                                       where _payment.year == item.Key
                                                                       select _payment).ToList()
                                                    select mypayments1).ToList();
    

    【讨论】:

      【解决方案4】:
      var groups = list.GroupBy(x => x.ID);
      

      谁能建议如何在这种情况下从 IGrouping 获取值(列表)?

      "IGrouping group" 实际上是一个带有键的 IEnumerable,所以你要么:

      • 迭代组或
      • 使用 group.ToList() 将其转换为 List
      foreach (IGrouping<int, smth> group in groups)
      {
         var thisIsYourGroupKey = group.Key; 
         List<smth> list = group.ToList();     // or use directly group.foreach
      }
      

      【讨论】:

        【解决方案5】:

        上述答案的更清晰的版本:

        IEnumerable<IGrouping<int, ClassA>> groups = list.GroupBy(x => x.PropertyIntOfClassA);
        
        foreach (var groupingByClassA in groups)
        {
            int propertyIntOfClassA = groupingByClassA.Key;
        
            //iterating through values
            foreach (var classA in groupingByClassA)
            {
                int key = classA.PropertyIntOfClassA;
            }
        }
        

        【讨论】:

        • 我不这么认为,至少现在是这样。
        【解决方案6】:
        foreach (var v in structure) 
        {     
            var group = groups.Single(g => g.Key == v. ??? );
            v.ListOfSmth = group.ToList();
        }
        

        首先,您需要选择所需的组。然后就可以在组上使用ToList 方法了。 IGrouping 是值的IEnumerable

        【讨论】:

          【解决方案7】:

          根据 IGrouping 的定义:

          IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable
          

          你可以像这样遍历元素:

          IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.ID)
          foreach(IEnumerable<smth> element in groups)
          {
          //do something
          }
          

          【讨论】:

          • 遍历组而不是组中的项目
          • 哦,我的错,然后:'foreach (smth p in groups.SelectMany(sth => sth))'
          猜你喜欢
          • 2013-12-03
          • 2011-06-15
          • 1970-01-01
          • 2015-11-20
          • 1970-01-01
          • 1970-01-01
          • 2011-01-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多