0

I'm rather new to MVC/C# and from what I understand, foreach is read-only.

I would like there to be a method that sets the values that are null to false if that method is called.

IQueryable<CurrentMatch> FindTheMatch = (from row in db.CurrentMatches
                                                 where row.UserId.ToString() == UserIdentity
                                                 where row.MatchID == MatchIdentity
                                                 select row);

        List<CurrentMatch> SetRemainingValuesToFalse = FindTheMatch.ToList();

I know that the part below wont work, it just demonstrates how I'm trying to achieve what I want to do.

        foreach (var Column in SetRemainingValuesToFalse)
        {
            if (Column == null)
            {
                Column = false;
            }
        }

As the row has a large number of properties it wouldn't be scaleable in the future to set each property manually.

user9993
  • 5,833
  • 11
  • 56
  • 117
Thomas Fonn
  • 113
  • 1
  • 2
  • 11

4 Answers4

2

You just need to use a standard for loop instead of a foreach. You can't modify the collection inside a foreach because that is how the iterator works. You can however modify values on the objects themselves.

See also: Changing objects value in foreach loop?

Community
  • 1
  • 1
user9993
  • 5,833
  • 11
  • 56
  • 117
1

I think you have this sort of the wrong way round. If you set that value to false inside any sort of loop, the context is lost when you exit that iteration of the loop.

Instead, what you probably want to do is, when consuming the list, treat nulls as false. You can use the null coalesce operator for this (??)

foreach (var row in FindTheMatch)
{
    DoSomethingInterestingWith(row.Column ?? false); // pass false if Column is null.
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • how can Column ever be null? Either the query returns matching rows or none. – alwayslearning Aug 23 '16 at 09:14
  • @alwayslearning - Im thinking that was a contrived example. I suspect that what the OP really meant is a *column* from the row is null, and they want to set any nulls in that column to false. The same answer applies. – Jamiec Aug 23 '16 at 09:22
  • @Jamiec: You are correct, it is the *columns* in a row, and not the row itself I would like to alter. I'll try to do it the way you explained asap. – Thomas Fonn Aug 23 '16 at 09:36
  • @ThomasFonn yeah thought so. See update, it probably more closely matches what you're doing. – Jamiec Aug 23 '16 at 09:37
0
for(int i=0;i<SetRemainingValuesToFalse.length;i++)
{
    if (SetRemainingValuesToFalse[i] == null)
    {
        SetRemainingValuesToFalse[i] = false;
     }
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
shady youssery
  • 430
  • 2
  • 17
0

you are slightly misunderstanding how the foreach is working

foreach(var c in col)

reads as

While col.asEnumerable.HasValues let c = col.asEnumerable.Current

because of this you can't change either the enumerable or its current value with out breaking the loop, however if the enumerable isn't attached to the collection you are changing then you have no problems

ToList for example will clone the collection meaning the enumerable is attached to the clone not the original collection

foreach(var c in col)
    col.Remove(c);

will error

foreach(var c in col.ToList())
    col.Remove(c);

works fine

like wise

foreach(var c in col)
    if(c.Field == null) c.Field = false;

is also fine because you are editing the the content of the current enumerable location not the location itself

however your stated desire of just replacing nulls in a collection is much simpler

col.Select(c=>c??false); //c#6
col.Select(c=>c == null? false : c); //c#<6

as you seem to be working with something akin to a datatable then you could do this

foreach(var row in table.Rows)
    foreach(var col in table.Columns)
        row[col] = row[col] ?? false;
MikeT
  • 5,398
  • 3
  • 27
  • 43