c# - Refactor nested 'foreach' and 'If' to LINQ -


I'm learning some LINQ. When something like this:

  foreach (resulting in var user items) {if (! (User.UserId.Equals (session.UserId))) {user.CanBeDeleted = true; }}  

How can this be solved with LINQ? Is it better to perform LINQ or foreach-if combinations?

In most cases LINQ does not program you go fast, but it will make your code more readable and So makes it easy to maintain

But if you need to modify something then LINQ is not the right tool. Use a simple loop to modify Q to query it. But you can filter to modify it:

  var toBeDeleted = result.Items.Where (U => u.UserId! = Session.UserId);  

You can now use a loop to execute it and change the user :

  foreach (User toBeDeleted user) .CanBeDeleted = true;  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -