Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从iGroup中获取值_C#_Linq_Select_Igrouping - Fatal编程技术网

C# 如何从iGroup中获取值

C# 如何从iGroup中获取值,c#,linq,select,igrouping,C#,Linq,Select,Igrouping,我有一个关于I分组和Select()方法的问题 假设我得到了一个IEnumerable,如下所示: var groups = list.GroupBy(x => x.ID); 其中列表是一个列表 现在我需要以某种方式将每个I分组的值传递给另一个列表: foreach (var v in structure) { v.ListOfSmth = groups.Select(...); // <- ??? } foreach(结构中的var v) { v、 ListOfSmth

我有一个关于
I分组
Select()
方法的问题

假设我得到了一个
IEnumerable
,如下所示:

var groups = list.GroupBy(x => x.ID);
其中
列表
是一个
列表

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

foreach (var v in structure)
{
    v.ListOfSmth = groups.Select(...); // <- ???
}
foreach(结构中的var v)
{
v、 ListOfSmth=组。选择(…);//

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

由于
iGroup
实现了
IEnumerable
,您可以使用
SelectMany
将所有
IEnumerable
放回一个ode>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();
List List=新列表();
IEnumerable groups=list.GroupBy(x=>x.id);
IEnumerable smths=组。选择多(组=>group);
List newList=smths.ToList();
下面是一个构建/运行的示例:


此解决方案的视频评论:

来自iGroup的定义:

IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable
i分组:IEnumerable,IEnumerable
您可以像这样迭代元素:

IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.ID)
foreach(IEnumerable<smth> element in groups)
{
//do something
}
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();
IEnumerable groups=list.GroupBy(x=>x.ID)
foreach(组中的IEnumerable元素)
{
//做点什么
}

以上答案的更清晰版本:

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;
    }
}
IEnumerable groups=list.GroupBy(x=>x.PropertyIntOfClassA);
foreach(var groupingByClassA分组)
{
int propertyIntOfClassA=groupingByClassA.Key;
//遍历值
foreach(groupingByClassA中的var classA)
{
int key=classA.PropertyIntOfClassA;
}
}
在这种情况下,有人能建议如何从iGroup中获取值(列表)吗

foreach (var v in structure) 
{     
    var group = groups.Single(g => g.Key == v. ??? );
    v.ListOfSmth = group.ToList();
}
“IGrouping group”实际上是一个带键的IEnumerable,因此您可以:

  • 对组或组进行迭代
  • 使用group.ToList()将其转换为列表
foreach(在组中分组)
{
var thisIsYourGroupKey=group.Key;
List List=group.ToList();//或直接使用group.foreach
}

假设您有MyPayments类,如

 public class Mypayment
{
    public int year { get; set; }
    public string month { get; set; }
    public string price { get; set; }
    public bool ispaid { get; set; }
}
你有我的付款清单吗

public List<Mypayment> mypayments { get; set; }
公共列表mypayments{get;set;}
您希望按年度对列表进行分组。您可以像这样使用linq:

IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.ID)
foreach(IEnumerable<smth> element in groups)
{
//do something
}
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();
List mypayments=(来自yearGroup中的iGroup项目
让mypayments1=(来自UserProjects.mypayments中的_payment
其中_payment.year==item.Key
选择_payment).ToList()
选择mypayments1.ToList();

遍历组而不是组中的项目哦,我错了,那么:“foreach(smth p in groups.SelectMany(sth=>sth))”ID通常是一个标识字段,应该是唯一的,如果您只是试图删除重复的数据,那么按它进行分组就没有用了。如果它是list.GroupBy(x=>x.SubID)然后使用分组是有意义的,但是在这种情况下,您很可能希望保留分组,并且foreach(var grp in groups){grp.ToList();}将执行此操作,这与普通的“orderBy”不同吗?List newList=List.orderBy(x=>x.id).ToList();@MorgoZ,
orderBy(x=>x.id)
将按照ID的升序对它们进行排序。将其与
.GroupBy(x=>x.ID)进行对比。选择many(group=>group)
,它将按照ID第一次出现的顺序对它们进行排序。如果原始ID的顺序为:[1,3,2,2,2,2,3,0],则将它们分组,然后展平为一个列表,将把ID放入新的顺序:[1,3,3,2,2,0].它对我不起作用..它说类型不能由usagetype推断usage@zcoop98,不。这不起作用。这将给你每个组中的第一个元素,这不是期望的结果:我不这么认为,至少现在是这样。