Concat()无法在C#中工作 [英] Concat() is not working in C#

查看:137
本文介绍了Concat()无法在C#中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个List类型的对象,我正在将另一个IEnumerable类型的集合合并到其中,但是在调试时该对象总是显示零记录。



我调试了,obj的计数为零

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;
}

推荐答案

一切都很好。我不得不使用AddRange()而不是Concat()

Everything was fine. I had to use AddRange() instead of Concat()
obj.AddRange(ob);


假设您可以编译 - 它不会如图所示 - 从调试器开始:在行上放置一个断点
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>();

并查看 param 的内容,以了解它包含的对象数量。



猜测,没有。没有对象,没有 foreach 正文执行,没有对象添加到集合中。但是您只能确认代码与数据一起运行 - 因此您需要调试器进行确认,然后使用堆栈跟踪找出原因。但即使这样,Concat也不会修改原始集合,所以你需要保存结果才能到达任何地方。



对不起,但我们做不到为你服务!

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!


这篇关于Concat()无法在C#中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆