本文介绍了如何用group by子句拆分列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<StateViewBenWise> listStateViewBenWise = new List<StateViewBenWise>();
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "March-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Feb-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Jan-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Dec-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Nov-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Oct-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ASHA", Count = "10", MonthYear = "Sep-2015" });

           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "March-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Feb-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Jan-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Dec-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Nov-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Oct-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "MOTHER", Count = "10", MonthYear = "Sep-2015" });

           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "March-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Feb-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Jan-2016" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Dec-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Nov-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Oct-2015" });
           listStateViewBenWise.Add(new StateViewBenWise() { BenType = "ANM", Count = "10", MonthYear = "Sep-2015" });


foreach (List<StateViewBenWise> itemBenType in listStateViewBenWise.GroupBy(x => x.BenType))





我想首先在itemBenType中输出所有ASHA然后是母亲然后ANM



我尝试过:



foreach(List< stateviewbenwise> itemBenType in listStateViewBenWise.GroupBy(x => x.BenType))





需要使用Yield



I want output in itemBenType first time all ASHA then MOTHER then ANM

What I have tried:

foreach (List<stateviewbenwise> itemBenType in listStateViewBenWise.GroupBy(x => x.BenType))


need to use Yield

推荐答案

foreach (var grouping in listStateViewBenWise.GroupBy(x => x.BenType))
{
   string benType = grouping.Key; // valid for all items below:
   foreach (StateViewBenWise item in grouping)
   {
      string count = item.Count;
      string monthYear = item.MonthYear;
   }
}


List<StateViewBenWise> CompleteList = new List<StateViewBenWise>();

           var groupingResult = listStateViewBenWise.GroupBy(k => k.BenType);
           foreach (var grouping in groupingResult)
           {
               string benchType = grouping.Key;
               List<StateViewBenWise> eachGroupResult  = grouping.ToList();
               CompleteList.AddRange(grouping);
           }


List<StateViewBenWise> CompleteList = new List<StateViewBenWise>();

           var groupingResult = listStateViewBenWise.GroupBy(k => k.BenType);
           foreach (var grouping in groupingResult)
           {
               string benchType = grouping.Key;
               List<StateViewBenWise> eachGroupResult  = grouping.ToList();
               CompleteList.AddRange(grouping);
           }


这篇关于如何用group by子句拆分列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 04:53