3

I have a method:

public List<AxeResult> LoadAxes(string kilometriczone, string axe)
{
    IEnumerable<AxeResult> allAxes = PullAxes();
    var findAxes = allAxes.Select(a => a.KilometricZone.StartsWith(kilometriczone) || a.KilometricZone.StartsWith(axe));

    return findAxes.Cast<AxeResult>().ToList();
}

I have this error:

IEnumerable<bool> does not contain a definition for ToList and the best extension method overload Enumerable.ToList<AxeResult> ( IEnumerable<AxeResult>) requires a receptor type IEnumerable<AxeResult>

I want to return a List of AxeResult after the search operation.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Jmocke
  • 269
  • 1
  • 10
  • 20
  • 4
    You use `Select` with `StartsWith`, which returns a `bool`. Maybe you want to use a where statement instead? – wimh Apr 15 '16 at 11:47
  • 2
    That error message is odd. I would have expected a run time `InvalidCastException` about not being able to cast `bool` to `AxeResult`. – juharr Apr 15 '16 at 12:00

2 Answers2

7

What you want is to filter the collection. That's what Enumerable.Where is for:

public List<AxeResult> LoadAxes(string kilometriczone, string axe)
{
    return PullAxes()
        .Where(a => a.KilometricZone.StartsWith(kilometriczone) || 
                    a.KilometricZone.StartsWith(axe))
        .ToList();
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

Some explanation in addition to the given answers:

Where() method acts like a filter and returns a subset of the same set

Select() method makes projection and returns new set

There is comprehensive explanation about the diffrence between .Select() and Where() methods here.

Community
  • 1
  • 1
Tanya Petkova
  • 155
  • 1
  • 1
  • 7