简体   繁体   中英

Concatenate two list of different type

I have two Lists List<int> and List<SharedCroList> .

 public class SharedCroList
    {
        public int CRO1Id { get; set; }
        public int CRO2Id { get; set; }
    }

  List<int> _cro1ReceiptEmpId = _receiptList                                                
                                .Where(r => r.StudentRegistration.StudentWalkInn.CROCount == 1)
                                .Select(r => r.StudentRegistration.StudentWalkInn.Employee1.Id)
                                 .ToList();

//if walkinn is shared one
List<SharedCroList> _cro2ReceiptEmpId = _receiptList
                                       .Where(r =>  r.StudentRegistration.StudentWalkInn.CROCount == 2)
                                       .Select(r => new SharedCroList
                                           {
                                               CRO1Id=r.StudentRegistration.StudentWalkInn.Employee1.Id,
                                               CRO2Id=r.StudentRegistration.StudentWalkInn.Employee2.Id
                                           })
                                           .ToList();

My aim is to concatenate these two lists into one.How can I do that? I have tried the concatenate method but its not working??

you cannot concat 2 list with 2 different type. so you'd better to convert your first list (_cro1ReceiptEmpId) to second one , and use AddRange() to concat.

for example :

List <SharedCroList> temp = _cro1ReceiptEmpId.Select(x => new SharedCroList { CRO1Id = x, CRO2Id = 0 }).ToList();

List<SharedCroList> ConcatinatedList = _cro1ReceiptEmpId.AddRange(_cro2ReceiptEmpId);

Edit :
if you want your output become List so you need to convert your second list to 2 separate List<int> :

List<int> listOfCRO1Id = _cro2ReceiptEmpId.Select(x=>x.CRO1Id).ToList();
List<int> listOfCRO2Id = _cro2ReceiptEmpId.Select(x=>x.CRO2Id).ToList();

List<int> FinalList = listOfCRO1Id.Concat(listOfCRO2Id).Concat(_cro1ReceiptEmpId).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM