比如有这样一个list
list<product> aproduct=new list<product>();
for(int i=0;i<9;i++)
{
product thp=new product();
thp.id=i;
thp.name=i+100;
aproduct.add(thp)
}class product{
int id{get;set;}
int name{get;set;}
}这个时候,我只想要到这种结果
list<int> desproduct,他的值是这样的1
2
3
4
。就是相当于把list的其他列不要,只要我需要一列,组成一个新的list
注意,不要用循环for和foreach,用list本身的方法,不知道能不能实现

解决方案 »

  1.   

    list.Skip(1).Take(1)
    lst.FindAll(delegate(T t){return t.ID>2;}); 
      

  2.   

    var newlist = from p in list select p.id
      

  3.   

    一列
    var q= from p in list select p.id
    或select new {p.id,p.name}等
      

  4.   


    class product
            {
               public  int id { get; set; } //注意要申明为public
               public int name { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    List<product> aproduct = new List<product>();
                    for (int i = 0; i < 9; i++)
                    {
                        product thp = new product();
                        thp.id = i;
                        thp.name = i + 100;
                        aproduct.Add(thp);
                    }
                    //这就是你想要的结果:
                    List<int> listnew = aproduct.Select(a => a.id).ToList();
                }
            }
      

  5.   

     List<int> list2 = (from l in aproduct
                        select l.id).ToList();不用lamda表达式,这样写同样也可以
    慢慢你会linq的魅力
      

  6.   


    class product
            {
               public  int id { get; set; } //注意要申明为public
               public int name { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    List<product> aproduct = new List<product>();
                    for (int i = 0; i < 9; i++)
                    {
                        product thp = new product();
                        thp.id = i;
                        thp.name = i + 100;
                        aproduct.Add(thp);
                    }
                    //这就是你想要的结果:
                    for(int i=0;i<aproduct.count;i++)
                    {
                           int i += aproduct[i].id+"|"
                   }
                }
            }
      

  7.   

    本质上来说,所有的办法都必须循环。区别只是循环由库函数或者虚拟机实现还是自己实现。因为CPU是顺序执行的。
      

  8.   

    使用linq相对简单些
    如:
    var result=from p in aproduct
               select p.id;
    List<int> list=result.ToList();或方法实现List<int> list=aproduct.Select(p=>p.id);