日期:2014-05-20  浏览次数:20903 次

linq查询如何返回指定列生成匿名类.
var niming = from s in strlist select new { s.Studentid, s.Studentname }; //获取strlist中student对象的两个属性


  var niming = strlist.Select();//如何在这里通过select限制查询出的列..跟上面的一样,只是换了一种方式

------解决方案--------------------
C# code
//你上面是在转LIst?

new {m.Stuendid,m.Studentname}).ToList();

------解决方案--------------------
class Student 

public int Age { get; set; } 
public string Name { get; set; } 
public bool Sex { get; set; } 

private List <Student> GetStudents() 

List <Student> students = new List <Student> { 
new Student{ Name="a", Age=1, Sex =true}, 
new Student{ Name="b", Age=2, Sex =false}, 
}; 
return students; 

List <Student> tmp = GetStudents.Where(p=>p.Sex ==True); 
List<Student> studentList = GetStudents
var stu2 =from st in stulist
select new {name = st.Name, age= st.Age}