2

I'm trying to add one list to another . So I have this main list that I'm going to build. I loop through records and built a list and want this list to main list every time I loop. I'm trying to do this in C#. I'm using following code. Add() function is not working.I'm getting syntax error.

 IList<CgValDetail> cgValDetail = null;
            //Get cgValDetails for each control
            foreach (UiControlScreenMetaData tempUiControls in uiControls)
            {
                if (tempUiControls.CgValId == null)
                {
                    continue;
                }
                IList<CgValDetail> tempCgValDetail = Retrieval<CgValDetail>.Search(new { CgValId = tempUiControls.CgValId }).ToList();
                if (!tempCgValDetail.Any())
                {
                    _foundationService.LogBusinessError(null, new ParameterBuilder("CgValId", tempUiControls.CgValId), "Invalid_CgValId_found");
                    return false;
                }
                //Add tempCgValDetail List to main list which is cgValDetail
                cgValDetail.Add(tempCgValDetail);


            }
Jaspreet Deol
  • 115
  • 1
  • 1
  • 12

5 Answers5

1

Take a look at AddRange.

var firstList = new List<string>();
var secondList = new List<string>() { "a", "b" };

firstList.AddRange(secondList);

You mentioned that you don't have access to AddRange... The problem is that you're using an IList, which doesn't implement AddRange. Check this out for more on why: Why doesn't IList support AddRange

I would advise you to switch to List.

Community
  • 1
  • 1
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
0

Your cgValDetail is null. That's why when you add(...), you get syntax error.

Create a new IList<CgValDetail>

IList<CgValDetail> cgValDetail = new List<CgValDetail>();

Why didn't you use List<T> in stead of IList<T>?

anhtv13
  • 1,636
  • 4
  • 30
  • 51
0

You forgot to new it.

Just a simple example:

List<a> aList= new List<a>();
List<aList> List_aList = new List<aList>();
List_aList.add(new aList());

Here the link to same question.

Community
  • 1
  • 1
Robert Ling
  • 33
  • 1
  • 8
0

First, you need to instantiate cgValDetail as a new list, not a null.

Then you should decide if you want to declare cgValDetail as IList instead of a List. If yes, try this:

IList<CgValDetail> cgValDetail = new List<CgValDetail>();
//Get cgValDetails for each control
foreach (UiControlScreenMetaData tempUiControls in uiControls)
{
    if (tempUiControls.CgValId == null)
    {
        continue;
    }
    IList<CgValDetail> tempCgValDetail = Retrieval<CgValDetail>.Search(new { CgValId = tempUiControls.CgValId }).ToList();
    if (!tempCgValDetail.Any())
    {
        _foundationService.LogBusinessError(null, new ParameterBuilder("CgValId", tempUiControls.CgValId), "Invalid_CgValId_found");
        return false;
    }
    //Add tempCgValDetail List to main list which is cgValDetail
    ((List<CgValDetail>)cgValDetail).AddRange(tempCgValDetail);
}

But I wonder why just not using a List instead of IList.

List<CgValDetail> cgValDetail = new List<CgValDetail>();
//Get cgValDetails for each control
foreach (UiControlScreenMetaData tempUiControls in uiControls)
{
    if (tempUiControls.CgValId == null)
    {
        continue;
    }
    List<CgValDetail> tempCgValDetail = Retrieval<CgValDetail>.Search(new { CgValId = tempUiControls.CgValId }).ToList();
    if (!tempCgValDetail.Any())
    {
        _foundationService.LogBusinessError(null, new ParameterBuilder("CgValId", tempUiControls.CgValId), "Invalid_CgValId_found");
        return false;
    }
    //Add tempCgValDetail List to main list which is cgValDetail
    cgValDetail.AddRange(tempCgValDetail);
}
Blas Soriano
  • 566
  • 3
  • 16
0

Maybe, you can use "UNION" in Linq (if you don't really care about performance or result set is not big enough).

cgValDetail.Add(tempCgValDetail);   

change to

cgValDetail = cgValDetail.Union(tempCgValDetail.Select(a => a)).ToList();
Heinz Siahaan
  • 355
  • 2
  • 10