比如有这样一个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本身的方法,不知道能不能实现
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本身的方法,不知道能不能实现
lst.FindAll(delegate(T t){return t.ID>2;});
var q= from p in list select p.id
或select new {p.id,p.name}等
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();
}
}
select l.id).ToList();不用lamda表达式,这样写同样也可以
慢慢你会linq的魅力
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+"|"
}
}
}
如:
var result=from p in aproduct
select p.id;
List<int> list=result.ToList();或方法实现List<int> list=aproduct.Select(p=>p.id);