Thomas Fonn
Thomas Fonn

Reputation: 113

Alternative to setting values in foreach C#

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.

Upvotes: 0

Views: 1774

Answers (4)

MikeT
MikeT

Reputation: 5500

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;

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

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.
}

Upvotes: 1

shady youssery
shady youssery

Reputation: 440

for(int i=0;i<SetRemainingValuesToFalse.length;i++)
{
    if (SetRemainingValuesToFalse[i] == null)
    {
        SetRemainingValuesToFalse[i] = false;
     }
}

Upvotes: 0

user9993
user9993

Reputation: 6170

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?

Upvotes: 2

Related Questions