views:

56

answers:

2

I looked online for some references but didn't have too much luck. Hopefully it's just some simple thing I'm overlooking, but in my code I'm looping through the participant list and storing the queried results into the array. As you can tell, my foreach statement will just add the last element of the array since the first one is replaced.

How can I just add the value to the next array index. So, if there are [2] in the array, how can I make sure that this.physEntityPhysicalLeftEntityIDs contains [2] instead of just always [1]? If I need to clarify, let me know.

if (leftParticipants.Length >= 0) // Check to see if LeftParticipants exists
{
   for (int c = 0; c < leftParticipants.Length; c++)
   {
       var l_entityIDs = 
          from BioList in o_data.Descendants(bp + "physicalEntityParticipant")
          where BioList.Attribute(rdf + "ID").Value == leftParticipants[c].TrimStart('#')
          select new
          {
              o_entityIDs = BioList.Elements(bp + "PHYSICAL-ENTITY")
              .Select(entityID => entityID.Attribute(rdf + "resource").Value).ToArray()
          };

       foreach (var entity in l_entityIDs)
       {
           this.physEntityPhysicalLeftEntityIDs = entity.o_entityIDs;  // Set SmallMolecules from Left
       }
    }
}
+1  A: 

Well if you want to treat it like an array/list, all you have to do is

l_enityIDs.ToList()

and then .Add(new {o_entityIDs = foo})

If you want to add it to the IEnumerable, that requires an extension method that returns everything in the source enumberable, and a yield statement adding on your next value.

Steve
+1  A: 

If physEntityPhysicalLeftEntityIDs is an array, you'll need to initialize an index variable and increment it each time through the foreach loop:

int destIndex = 0;       
foreach (var entity in l_entityIDs)
{
    this.physEntityPhysicalLeftEntityIDs[destIndex] = entity.o_entityIDs;  // Set SmallMolecules from Left
    ++destIndex;
}

That assumes that you've allocated enough space in the array. If there are more items than will fit in the array, you're going to get an index out of bounds error.

To make sure there's enough space in the array, you can allocate it before the loop above:

this.physEntityPhysicalLeftEntityIds = new int[l_entityIDs.Count()];

Replace int in that line with the proper type (you didn't say what type is being stored in the array).

Jim Mischel
Good tip! The type is String[]... I think you meant new int[l_entityIDs.Count()];
dotnetdvlpr
Yes, you're right. Will fix.
Jim Mischel