Click here to Skip to main content
15,441,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can this be done in 1 statement for example using OR ||

_scheduleList.Where(c => c.TeamA == "as roma")
           .Select(c =>
           {
               c.TeamA = "roma";
               return c;
           })
           .ToList();

       _scheduleList.Where(c => c.TeamB == "as roma")
           .Select(c =>
           {
               c.TeamB = "roma";
               return c;
           })
           .ToList();


What I have tried:

tried several method but this is the best I can get to.
Posted
Updated 18-Aug-22 11:03am
Comments
Richard Deeming 19-Aug-22 4:15am    
The Select method is supposed to project the source. Adding side-effects to the projection is a really bad idea.

Not really - because you are setting a different field / property of the class when you find what you are looking for, you can't really combine the two tests.
You could combine the two result sets with a Union though:
C#
result = _scheduleList.Where(...).Union(_scheduleList.Where(...));
 
Share this answer
 

If _scheduleList is a Generic.List you can simply use the ForEach extension to update the list.

C#
_scheduleList.ForEach(c =>
     {
       c.TeamA =  c.TeamA=="as roma" ? "roma" : c.TeamA;
       c.TeamB =  c.TeamB=="as roma" ? "roma" : c.TeamB;
     });

If you wish just to select the items that have been updated, you can do something like this.
C#
var updatedItemsList = _scheduleList.Where(c =>  c.TeamA=="as roma"  || c.TeamB=="as roma")
                                    .Select(c => c).ToList();
 updatedItemsList.ForEach(c =>
 {

   c.TeamA =  c.TeamA=="as roma" ? "roma" : c.TeamA;
   c.TeamB =  c.TeamB=="as roma" ? "roma" : c.TeamB;
 });

 
Share this answer
 
Comments
Richard Deeming 19-Aug-22 4:16am    
And if it's not a List<T>, you can just use a foreach loop instead. :)
George Swan 19-Aug-22 4:48am    
Yes indeed but it is not as neat

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900