在foreach循环中连接列表中的项目

时间:2014-06-26 10:39:05

标签: linq list

我想在foreach循环中使用相同列表加入列表,最后生成完整列表。我尝试使用x = x.Concat(x);和x = x.AddRange(x);但还没有解决。 请告诉我我在哪里做错了或者有更简单的方法来做到这一点

try
            {
                var dc = new OnyxDBEntities();
                int list2 = 0;
                int list3 = 0;
                var count = dc.Users.Select(i => i.Id);
                //var y = (from users in dc.Users where users.Id == 2 
                 select new { username = users.UserName, 
                fullname = users.FirstName + " " + users.LastName, 
                userid = users.Id, beadsetsa =  users.BeadSets, 
                designaa = users.Designs }).ToList(); 
                var y = (from users in dc.Users select 
                new { username = users.UserName,
                fullname = users.FirstName + " " + users.LastName,
                userid = users.Id, beadsetsa = list2, designaa = list3 }).ToList(); 
                var x = y;
                foreach (var i in count)
                {
                     list2 = (from users in dc.BeadSets 
                     where users.UserId == i select users).Count();
                     list3 = (from users in dc.Designs 
                     where users.UserId == i select users).Count();
                     x = (from users in dc.Users
                         where users.Id == i select 
                     new { username = users.UserName,
                     fullname = users.FirstName + " " + users.LastName,
                     userid = users.Id, beadsetsa = list2, designaa = list3 }).ToList();
                     x = x.Concat(x);


                }

                CreateExcelFile.CreateExcelDocument(x, "C:\\Customers1.xlsx");
            }
            catch (Exception ex)
            {
            }

我收到此错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<AnonymousType#1>'

0 个答案:

没有答案