Alexandr Sulimov
Alexandr Sulimov

Reputation: 1924

C# Generic.ForEach not work? or EF easy way to exclude properties

I need clear some propery from List

CategoryAccount is class

Get List

List<CategoryAccount> ret = context.CategoryAccounts.ToList();

Clear with ForEach

//Clear Accounts poperty to null
//Accounts is List<Acccount>
ret.ForEach(x => x.Accounts = null);
//Clear Owner poperty to null
//Owner is class Owner 
ret.ForEach(x => x.Owner = null);

//In result
ret[0].Account != null
ret[0].Owner != null

Or exclude property in context.CategoryAccounts.

I don't want use Select(x => new { prop1 = x.prop1, prop2 = x.prop2? ///} - too many properties in model must be included.

Upvotes: 0

Views: 136

Answers (1)

Kaspars Ozols
Kaspars Ozols

Reputation: 7017

You seem to be using lazy loading. You have to trigger load before assigning any values to navigation properties. You can do it using Include.

List<CategoryAccount> ret = context.CategoryAccounts
    .Include(x => x.Accounts)
    .Include(x => x.Owner)
    .ToList();
//Clear with ForEach

//Clear Accounts poperty to null
//Accounts is List<Acccount>
ret.ForEach(x => x.Accounts = null);
//Clear Owner poperty to null
//Owner is class Owner 
ret.ForEach(x => x.Owner = null);

Upvotes: 5

Related Questions