日期:2014-05-18 浏览次数:20427 次
public class MainClass { class Program { static void Main(string[] args) { List<Vo> list = new List<Vo>(); Vo vo1 = new Vo(); vo1.ID = 1; vo1.name = "aa"; vo1.comm = "comaa"; list.Add(vo1); Vo vo2 = new Vo(); vo2.ID = 2; vo2.name = "bb"; vo2.comm = "combb"; list.Add(vo2); //------------上面是添加------------ //方法一 foreach (Vo vo in list) { Console.WriteLine(vo.ID + "-" + vo.name + "-" + vo.comm); } //方法二 for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i].ID + "-" + list[i].name + "-" + list[i].comm); } Console.ReadLine(); } } /// <summary> /// 你的实体类 /// </summary> public class Vo { public int ID { get; set; } public string name { get; set; } public string comm { get; set; } } }
------解决方案--------------------
取list中第n个实体的数据:
list[n-1].ID,
list[n-1].name,
list[n-1].comm
------解决方案--------------------
var result = list.Select(x => x.name); //取名字集合
var result1 = list.Select(x => new { x.ID, x.name }); //取得id和名字两列
var result2 = result1.Take(10); //取得 result1 前10条记录
var result3 = list.Max(x => x.ID); //取得 ID 最大值
var result4 = list.Where(x => x.name == "xxx"); //取得名字为xxx的记录
...
------解决方案--------------------
foreach(var item in list){}
妥妥的