我怎么能Concat三个列表或不可数? [英] How Can I Concat Three List Or Ienumerable?

查看:76
本文介绍了我怎么能Concat三个列表或不可数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var datasource = new NORTHWNDEntities().Categories.ToList();
       var datasource1 = new NORTHWNDEntities().Customers.ToList();
       var datasource2 = new NORTHWNDEntities().Orders.ToList();

推荐答案

请参阅以下代码:



See the following code:

class Program
{
    static void Main(string[] args)
    {
        List<int> a = new List<int>();
        a.Add(1);
        a.Add(2);

        List<int> b = new List<int>();
        b.Add(3);
        b.Add(4);

        List<int> c = new List<int>();
        c.Add(5);
        c.Add(6);

        List<int> concat = new List<int>(a.Concat(b).Concat(c));

        for (int i = 0; i < concat.Count; ++i)
        {
            Console.WriteLine(concat[i]);
        }
        Console.ReadKey();
    }
}


在你的代码中,

In your code,
var datasource = new NORTHWNDEntities().Categories.ToList();
var datasource1 = new NORTHWNDEntities().Customers.ToList();
var datasource2 = new NORTHWNDEntities().Orders.ToList();



如果这三行是正确的,数据源,datasource1和datasource2是IEnumerable,那么你的代码不应该产生任何错误。您是否收到此行中的错误?


If these three lines are correct and datasource, datasource1 and datasource2 are IEnumerable then your code shouldn't produce any error. Are you getting the error in this line?

ViewData["datasource"] = Datasource;



新的NORTHWNDEntities()。Categories.ToList(); 返回一个 IEnumerable ,那么你的代码不会产生错误。



并且连续三个IEnumberables,以下代码有效(Soln 1中的代码修改):


Does new NORTHWNDEntities().Categories.ToList(); returns a IEnumerable, then your code shouldn't produce errors.

And to concat three IEnumberables, the following code works(a modification of code in Soln 1):

List<int> a = new List<int>();
a.Add(1);
a.Add(2);

List<int> b = new List<int>();
b.Add(3);
b.Add(4);

List<int> c = new List<int>();
c.Add(5);
c.Add(6);

IEnumerable<int> a2 = a.Cast<int>();
IEnumerable<int> b2 = b.Cast<int>();
IEnumerable<int> c2 = c.Cast<int>();

IEnumerable<int> concat = a2.Concat(b2).Concat(c2);



另外,不要使用 var 总是。我不反对 var ,但在使用之前你应该知道对象的类型。


And also, don't use var always. I'm not against var, but you should know the type of the object before you use it.


这篇关于我怎么能Concat三个列表或不可数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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