Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an object of type List and I am concatinating another collection of type IEnumerable into it but the the object always showed zero record while debugging.

When I debugged, the Count for obj was zero
obj = 0
Where have I gone wrong? Or is there another way to do it? I even tried Add(), but it didn't work

What I have tried:

public IEnumerable<Dealer> GetDealerData(param)
{
    var obj = new List<Dealer>();

    foreach(var item in param)
    {
        var ob = ObjectFactory.GetInstance<IDealerRepository>().MyFunc(item.Id);
        //ob is of type IEnumerable<Dealer>

        obj.Concat(ob);
    }

    return obj;
}
Posted
Updated 19-Feb-19 3:10am
v2

Everything was fine. I had to use AddRange() instead of Concat()
obj.AddRange(ob);
 
Share this answer
 
Comments
Richard Deeming 20-Feb-19 9:15am    
Indeed - Concat[^] is an extension method which returns the concatenated sequence. It doesn't modify the input sequence in any way.
Assuming you can get that to compile - it won't as shown - start with the debugger: put a breakpoint on the line
var obj = new List<Dealer>();
and look at the content of param to find out how many objects it contains.

At a guess, none. No objects, no foreach body execution, no objects added to the collection. But you can only confirm that with your code running with your data - so you need the debugger to confirm, and then use the stack trace to find out why. But even then, Concat does not modify the original collection, so you'd need to save the result to get anywhere.

Sorry, but we can't do that for you!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900