You can use method calls and write a ForEach or ForEachWithContinue method that lets you change each element, but EF doesn't know what to do with it anyway, and you will need to use ToList to pull items from EF before you can do anything them to do.
ForEach example (functional purists will not like this, of course):
public static void ForEach<T>(this IEnumerable<T> pEnumerable, Action<T> pAction) { foreach (var item in pEnumerable) pAction(item); } public static IEnumerable<T> ForEachWithContinue<T>( this IEnumerable<T> pEnumerable, Action<T> pAction ) { foreach (var item in pEnumerable) pAction(item); return pEnumerable; }
Then:
EFContext .SomeTable .Where(x => x .id == 1) .ToList()
(In fact, List<T> already has a ForEach method, so writing IEnumerable extensions in this case is not absolutely necessary.)
In principle, EF needs to pull data into memory in order to know that you have changed something, to find out what your changes are, and to know what needs to be saved in order to return to the database. I would also like to consider what you are trying to do when you are overwriting data that neither the user nor the program even considered. How did you determine that it was the data that you would like to overwrite first?
In addition, you can directly write direct SQL queries directly to the database using the ExecuteStoreCommand method, which will be the “normal” way to do this. Sort of:
EFContext.ExecuteStoreCommand( "UPDATE SomeTable SET Name = {0} WHERE ID = {1};", "NewName", 1 );
source share