0

I have two collections, both contain objects.

First one is IList and the second one is Dictionary.

I need to traverse through IList and if the condition is filled then activate method from the certain object which is stored in Dictionary.

The current situation is like this:

 foreach (MyObject mo in MyListOfObjects)
 {
      if (mo.Active == myStatus.Enabled)
      {
           DictList[mo.ID].Start();
      }
  }

So far i've done this:

var r = MyListOfObjects.Where(mo => mo.Active == myStatus.Enabled);

But I have no idea how to include in this DictList[mo.ID].Start();

Mohammad Roshani
  • 486
  • 7
  • 19
Josef
  • 2,648
  • 5
  • 37
  • 73
  • 4
    Why do you want this? There´s absoluetely no gain in using Linq here. Is just another syntax. Apart from this the Q in Linq stand for **querying**, not **modifying** or whatever you want to do in your `Start`. – MakePeaceGreatAgain Feb 28 '19 at 12:38
  • You could just remove the explicit test inside the loop if you really think that this is more readable. No effective gain _foreach(MyObject mo in MyListOfObjects.Where(o => o.Active == myStatus.Enabled)) DictList[mo.ID].Start();_ – Steve Feb 28 '19 at 12:40
  • *need to traverse... is stored in Dictionary* well if your traversing a dictionary why is it in a Dictionary? – Liam Feb 28 '19 at 12:41
  • If you include a `Select(mo => DeictList[mo.ID])` then you can loop over that result and call your method `foreach(var x in r) x.Start();`. – juharr Feb 28 '19 at 12:42

3 Answers3

1

Not a great use of linq, but you could filter the list using linq then loop through it.

var itemsToStart = MyListOfObjects.Where(mo => mo.Active == myStatus.Enabled)
    .Select(mo=> DictList[mo]); //or ToList() if you intend to re-iterate 

foreach (var itemToStart in itemsToStart) {
    itemToStart.Start();
}
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
0

If anything at all, just remove the inner if

foreach (MyObject mo in MyListOfObjects.Where(x => x.Active == myStatis.Enabled))
{
     DictList[mo.ID].Start();
}

But that is all you should do - it is perfectly readable and maintainable.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
-2
MyListOfObjects.Where(mo => mo.Active == myStatus.Enabled).ToList().ForEach(mo => DictList[mo.ID].Start());

ForEach is found: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=netframework-4.7.2

Candide
  • 30,469
  • 8
  • 53
  • 60
  • 1
    `ForEach` is only on a list, so this wont even work. And there is no benefit converting to a list just to call `ForEach` – Jamiec Feb 28 '19 at 12:42
  • 1
    The built-in `ForEach` method is on `List` , not on `IEnumerable`, so this won't compile. A `ToList` call is needed, and it's questionable at best if that's a good option. – Alejandro Feb 28 '19 at 12:43
  • 1
    His object is called `MyListOfObjects` so I guess, it is a `List` :) – Candide Feb 28 '19 at 12:43
  • 2
    @Candide and when you call `Where(...)` you're turning it to an `IEnumerable` – Jamiec Feb 28 '19 at 12:44
  • 1
    @Candide It may be, but you've called `Where` afterwards, which returns an `IEnumerable`. – Alejandro Feb 28 '19 at 12:44
  • Ah, yes, I forget. I generally have my own extension for ForEach on IEnumerable – Candide Feb 28 '19 at 12:46
  • 1
    Here's why there isn't a `ForEach` for `IEnumerable` https://blogs.msdn.microsoft.com/ericlippert/2009/05/18/foreach-vs-foreach/ – juharr Feb 28 '19 at 12:48
  • @juharr That's an argument but not one I agree with. "When we provide two subtly different ways to do exactly the same thing, we produce confusion in the industry." I am not confused. Come on, linq extensions in themselves shouldn't have been written in the first place if we were to agree with that statement. – Candide Feb 28 '19 at 12:53
  • 1
    Also should be noted that `ToList().ForEach` will first iterate the Linq query to create a intermediate list, then iterate that to call the code in the `ForEach` and throw away that list. – juharr Feb 28 '19 at 12:54
  • 1
    I know, but really, this guy is a new programmer. Do you really think that he is going to face any performance issues by iterating twice? – Candide Feb 28 '19 at 12:55
  • 1
    @Candide I think the bigger take away is that it is less readable and it's not just about if you are confused it's if another programmer that has to look at your code is. You have to remember that half the reason for code is to express to other programmers what you're trying to do. – juharr Feb 28 '19 at 12:55