stuLists.foreach(o=>Response.Write("姓名:"+o.Name+",年龄:"+o.Age+"\n")); ------解决方案-------------------- public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Student(int id, string name, int age)
{
this.ID = id;
this.Name = name;
this.Age = age;
}
}
public string MyLinq()
{
string str_return = string.Empty;
List<Student> Mylist = new List<Student>();
Mylist.Add(new Student(1, "yh", 20));
Mylist.Add(new Student(2, "yh", 20));
Mylist.Add(new Student(3, "yh", 20));
Mylist.Add(new Student(4, "yy", 20));
Mylist.Add(new Student(1, "qb", 10));
var d1 = from stu in Mylist where stu.Age == 20 select stu;
var d2 = from stu in Mylist where stu.Name.Contains("qb") select stu;
foreach (Student s in d2)
{
str_return += " " + string.Format("{0},{1},{2}", s.ID, s.Name, s.Age)+" ";
}
return str_return;
}