user3276223
user3276223

Reputation: 59

Get data out of foreach loop

I need to try and get string itemOUT out of a foreach loop. How can I achieve this? I tried refactoring to a method with no success.

var modalTypeItemData = modalGpData.Descendants(occ + "ItemData")
                        .Where(x => (string)x.Attribute("ItemOID") == "I_TABLE_MODAL_TYPE_TABLE")
                        .Select(_ => _.Attribute("Value").Value);
foreach (var modaltypeitem in modalTypeItemData)
{
    row["Modality Type"] = modaltypeitem;
    string itemOUT = modaltypeitem;
}

Upvotes: 0

Views: 3694

Answers (1)

Glorfindel
Glorfindel

Reputation: 22641

You can get itemOUT out of the loop with this code:

string itemOUT = null;
foreach (var modaltypeitem in modalTypeItemData)
{
    row["Modality Type"] = modaltypeitem;
    itemOUT = modaltypeitem;
}

But this will only give you the modal type item of the last element. I'm not sure if this is what you need.

If you need all items, you need a List<string> or an array (string[]). This could be done with:

var itemsOUT = modalTypeItemData.Select(item => item.modaltypeitem).ToList();

Upvotes: 2

Related Questions