添加项目从LINQ列出变种 [英] Add items to list from linq var

查看:119
本文介绍了添加项目从LINQ列出变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

 公共类CheckItems 
{
公共字符串描述{得到;组; }
公共字符串ActualDate {搞定;组; }
公共字符串TargetDate {搞定;组; }
公共字符串值{搞定;组; }
}



名单,LT; CheckItems> vendlist =新的List< CheckItems>();

VAR vnlist =(从上在spcall
其中up.Caption ==联系
选择新CheckItems
{
说明= up.Caption ,
TargetDate =的String.Format({0:MM / DD / YYYY},up.TargetDate),
ActualDate =的String.Format({0:MM / DD / YYYY}, up.ActualDate)
值= up.Value
})了ToList();



//接下来,当我尝试添加vnlist到vendlist,我得到一个错误,因为我不能添加这个到列表中,我得到和错误说我有一些无效的参数

  vendlist.Add(vnlist); 


解决方案

如果你需要的元素任何IEnumerable集合添加到你需要使用列表中 的AddRange

  vendlist.AddRange(vnlist); 


I have the following query:

    public class CheckItems
    {
        public String Description { get; set; }
        public String ActualDate { get; set; }
        public String TargetDate { get; set; }
        public String Value { get; set; }
    }



   List<CheckItems>  vendlist = new List<CheckItems>();

   var vnlist = (from up in spcall
               where up.Caption == "Contacted"
                      select new CheckItems
                      {
                          Description = up.Caption,
                          TargetDate = string.Format("{0:MM/dd/yyyy}", up.TargetDate),
                          ActualDate = string.Format("{0:MM/dd/yyyy}", up.ActualDate),
                          Value = up.Value
                      }).ToList();

// Next, when I try to add vnlist to vendlist, I get an error as I cannot add this to the list I get and error saying I have some invalid arguments

         vendlist.Add(vnlist);

解决方案

If you need to add any IEnumerable collection of elements to the list you need to use AddRange.

vendlist.AddRange(vnlist);

这篇关于添加项目从LINQ列出变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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