转换VAR列出< T>在c# [英] Convert var to List<T> in c#

查看:138
本文介绍了转换VAR列出< T>在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法 VAR 转换回列表< CustomObject方式>

 私人无效PopulateChart(列表< CustomObject> RAWDATA)
{
VAR groupedByCategory1 = rawData.GroupBy(CO => co.Category1)
//名单< CustomObject>数据= groupedByCategory1.GroupBy(CO => co.Category1).ToList();
}

请注意:从 RAWDATA 我得GROUPBY数据。再次使用这个结果我有组数据。


解决方案

  VAR数据= groupedByCategory1 
.GroupBy(CO => co.Category1)
。选择(GRP =>新建
{
组别= grp.Key //这是您用来关键组
GROUPLIST = grp.ToList(); //这将是CustomObject
}
.ToList()的名单;

其实, GROUPBY 组列表到另一个列表,其中组别是一样的。



您可以访问如下所示

 的foreach(数据VAR组)
{//这组列表与LT; CustomObject取代。如果你不想来访问它像这样,什么组由
VAR groupKey =组的含义是什么? .Category1;
的foreach(CustomObject MyObj中在group.groupList)
{

}
}


I am unable to convert back var to List<CustomObject>.

private void PopulateChart(List<CustomObject> rawData)
 {
   var groupedByCategory1 = rawData.GroupBy(co => co.Category1)
   //List<CustomObject> data = groupedByCategory1.GroupBy(co => co.Category1).ToList();    
 }

Note: From rawData I have to get Groupby data. using that result again I have to group the data.

解决方案

 var data = groupedByCategory1
         .GroupBy(co => co.Category1)
         .Select(grp => new 
              {
                  Category1 = grp.Key// This is the key that you used to group
                  groupList=grp.ToList();//This will be the list of CustomObject
              }
         .ToList(); 

Actually, GroupBy groups your list into another list where Category1 is same.

You can access it as shown below

foreach (var group in data)
{// This group is List<CustomObject>. If you don't want to access it like this, whats the meaning of group by?
  var groupKey=group.Category1;
  foreach(CustomObject myObj in group.groupList)
   {

   }
}

这篇关于转换VAR列出&LT; T&GT;在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆