Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerable.Concat not working on empty list [duplicate]

Tags:

c#

ienumerable

I have below code which retrieves a list from a subfunction and returns it back to a main function. My problem is I cannot concatenate the result from the subfunc to the enumerable declared in mainfunc. Is there something I am missing?

private void MainFunc()
{
    IEnumerable<CustomClass> keys = Enumerable.Empty<CustomClass>();
    foreach (var item in items)
    {
        keys.Concat(SubFunc(item));
    }
}
private IEnumerable<CustomClass> SubFunc (string y)
{
    IEnumerable<CustomClass> list = GetList(y).ToList();
    return list;
}
like image 288
jengfad Avatar asked Oct 17 '25 03:10

jengfad


1 Answers

.Concat() does not mutate the subject, but returns a new IEnumerable<T>.

You're just throwing it away.

Fix your code with:

keys = keys.Concat(SubFunc(item))
like image 70
spender Avatar answered Oct 19 '25 18:10

spender



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!