Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return List<object> from IEnumerable C#

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.

like image 449
Jmocke Avatar asked Feb 08 '26 03:02

Jmocke


1 Answers

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();
}
like image 50
Yuval Itzchakov Avatar answered Feb 09 '26 15:02

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!