2

Him

I have a List<MyObject> _users, and I would like to find all users which the userName begin by "toto" and set an attribute, for all of them, bool excepted = true.

I actually have this code:

_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London")

and I would like to reach something like this:

_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London").Cast<MyObject>().excepted = true;

The Cast to know which kind of object that I'm currently using, and then set my attribute.... This code obviously don't work at all, but I have no idea how could I do it and if it possible to do that :(

Thanks

nvoigt
  • 75,013
  • 26
  • 93
  • 142
wytes
  • 421
  • 7
  • 24

2 Answers2

1
_users.FindAll(z => z.userName.StartWith("toto") == true && location == "London")
      .ForEach(x=>x.excepted =true)
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • You gave me an orgasm ! ahah thank you so much ! :D I need more practice on these kind of queries :) – wytes Dec 08 '13 at 17:51
1
_users.Where(x => x.userName.Contains("toto")).ToList().ForEach(y => y.excepted = true);
tjheslin1
  • 1,378
  • 6
  • 19
  • 36
  • What is the best optimization between these results ? – wytes Dec 08 '13 at 17:55
  • http://stackoverflow.com/questions/1531702/findall-vs-where-extension-method, I found a cool topic about the optimization :) – wytes Dec 08 '13 at 18:14
  • 2
    Answer is valid but it won't optimize the code in anyway..`ToList` would create a `List` and so is exactly similar to `FindAll`..To really optimize it use foreach loop over `_users.Where` without using `ToList`(though it won't make any significant difference) – Anirudha Dec 08 '13 at 18:41