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();

4
  • 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. Commented Feb 28, 2019 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(); Commented Feb 28, 2019 at 12:40
  • need to traverse... is stored in Dictionary well if your traversing a dictionary why is it in a Dictionary? Commented Feb 28, 2019 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();. Commented Feb 28, 2019 at 12:42

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Which is completely the same OP already has (despite from iterating).
@HimBromBeere of course it is, it just avoids a bit of nesting
-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

11 Comments

ForEach is only on a list, so this wont even work. And there is no benefit converting to a list just to call ForEach
The built-in ForEach method is on List<T> , not on IEnumerable<T>, so this won't compile. A ToList call is needed, and it's questionable at best if that's a good option.
His object is called MyListOfObjects so I guess, it is a List :)
@Candide and when you call Where(...) you're turning it to an IEnumerable<WhateverListWas>
@Candide It may be, but you've called Where afterwards, which returns an IEnumerable<T>.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.