0

私は2つのリストを持っています。それらを1つのリストにまとめたい。

問題は、2 つのリストの 1 つが 1 つのカウント サイズだけであることです。

firstList.Count = 1

2 番目のリストのサイズは 2 です。

secondList.Count = 2

したがって、これらのリストのボットを 1 つのリストに結合したいと考えています。

megaList => firstList {0, Empty},
            secondList {0 , 2}

2 つのリストが同じサイズではないため、これを行うコードは機能しません。これを修正するにはどうすればよいですか?

 List<QuestionAndResponses> megaList = new List<QuestionAndResponses>();
                for (var i = 0; i < firstList.Count(); i++)
                {
                    megaList.Add(new QuestionAndResponses()
                    {
                        Responses = new List<Response>(firstList[i].Response),
                        Questions = new List<Question>(secondList[i].Questions)
                    });
                }

私のモデルは次のようになります。

public class QuestionAndResponses
    {
        public PreScreener Question { get; set; }
        public PreScreenerResponse Response { get; set; }
    }
4

2 に答える 2

0

なぜこれらの 2 つのリストがあり、そこに何を保存したいのか正確にはわかりません。しかし、コードを単純に変更するだけで、より大きなリストをループ処理しないのはなぜでしょうか?

List<QuestionAndResponses> megaList = new List<QuestionAndResponses>();
var biggerList = firstList.Count() > secondList.Count() ? firstList : secondList
for (var i = 0; i < biggerList.Count(); i++)
{
   var response = firstList.Count() >= i+1 ? new List<Response>(firstList[i].Response) : new List<Response>();
   var questions = secondList.Count() >= i+1 ? new List<Question>(secondList[i].Questions) : new List<Question>(); 

   megaList.Add(new QuestionAndResponses()
      {
         Responses = response,
         Questions = questions
      });
}

これがあなたが求めたものであることを願っています。

于 2013-11-03T17:16:51.603 に答える