caldera.sac
caldera.sac

Reputation: 5098

how to add new object for each item in the list

I have assigned data to a list like below.

   foreach (var items in niledetails)
            {
                cruiseDetails.Add(new CruiseDetails()
                {
                    mainID = items.cruiseID,
                    idL = items.idlength,
                    mainimageUrl = items.cruiseimageUrl,
                    adultPrice = items.adultprice,
                    location = items.cruiseLocation,
                    numberofDays = items.numberofdays,
                    description = items.description,
                    embarkationP = items.embarkationport,
                    cruiseTyp = items.cruisetype,
                    childPrice = items.childprice,
                    totalPrice = items.totalprice,
                    farecode = items.referenceNumber,
                    experience = items.cruiseName,
                    fullcategoryname = items.cruiseCabname
                });
            }

this works fine.this has 30 objects and for each object has 14 key/values.now what I want is to add additional value for each object in the list.

that means something like this

for(int i=0;i<cruiseDetails.Count();i++)
            {

                //for each object I want to add another key value( 15th) for all 30 objects.
            }

How can I do that. hope your help.

NOTE : I'm not looking for this. EX:

cruiseDetails.Insert(0, new someModel() { city = "All"});

Upvotes: 1

Views: 4860

Answers (4)

Kyght
Kyght

Reputation: 617

Set your properties in your loop

for(int i=0;i<cruiseDetails.Count();i++)
            {
               cruiseDetails[i].mynewproperty = somevalue;
            }

Upvotes: 0

Othello  .net dev
Othello .net dev

Reputation: 438

try something as this:

List<Tuple<int, CruiseDetails>> list = new List<Tuple<int, CruiseDetails>>();

CruiseDetails> sourceCruises = new List<CruiseDetails>();
int i = 0;
foreach (var c in sourceCruises)
{
    list.Add(new Tuple<int, CruiseDetails>(i, c));
    i++;
}

Upvotes: 0

TobiasR.
TobiasR.

Reputation: 801

Try this:

foreach (CruiseDetails myCruiseDetail in cruiseDetails){
  myCruiseDetail.myNewKey = myNewValue;
}

Upvotes: 1

Akshey Bhat
Akshey Bhat

Reputation: 8545

for(int i=0;i<cruiseDetails.Count();i++)
{

    cruiseDetails[i].additionalKey = val;
}

try this code

Upvotes: 2

Related Questions