片段一
class Student
{
public string name;
public int id;
//班级
public string className;
}
Student[] arr =
{
new Student {name = "zhangshan", id = 0, className = "二班"},
new Student {name = "lisi", id = 1, className = "二班"},
new Student {name = "wanger", id = 2, className = "一班"},
new Student {name = "wangsan", id = 3, className = "二班"},
new Student {name = "wangsi", id = 4, className = "三班"},
new Student {name = "wangwuc", id = 5, className = "四班"},
new Student {name = "wangliub", id = 6, className = "五班"},
new Student {name = "wangqib", id = 7, className = "二班"},
new Student {name = "wangba", id = 8, className = "六班"},
new Student {name = "wangjiu", id = 9, className = "三班"},
new Student {name = "wangshi", id = 10, className = "二班"}
};
//写法一
//var rst = from s in arr where s.name.StartsWith("wang")
orderby s.name[s.name.Length - 1] select s.name;
//写法二
var rst = arr.Where(s => s.name.StartsWith("wang")).
Select(s => s.name).OrderBy(s=>s[s.Length-1]);
System.Console.WriteLine("数目{0}", rst.Count());
foreach (var VARIABLE in rst)
{
System.Console.WriteLine(VARIABLE);
}
选择以wang打头的字符串,用最后一个字母排序,运行结果,
片段二
选择两列内容输出
class Student
{
public string name;
public int id;
//班级
public string className;
}
static void Main(string[] args)
{
Student[] arr =
{
new Student {name = "zhangshan", id = 0, className = "二班"},
new Student {name = "lisi", id = 1, className = "二班"},
new Student {name = "wanger", id = 2, className = "一班"},
new Student {name = "wangsan", id = 3, className = "二班"},
new Student {name = "wangsi", id = 4, className = "三班"},
new Student {name = "wangwuc", id = 5, className = "四班"},
new Student {name = "wangliub", id = 6, className = "五班"},
new Student {name = "wangqib", id = 7, className = "二班"},
new Student {name = "wangba", id = 8, className = "六班"},
new Student {name = "wangjiu", id = 9, className = "三班"},
new Student {name = "wangshi", id = 10, className = "二班"}
};
var rst = from s in arr where s.name.StartsWith("wang")
select new {s.name, s.className};
System.Console.WriteLine("数目{0}", rst.Count());
foreach (var VARIABLE in rst)
{
System.Console.WriteLine("name = {0}, className = {1}",
VARIABLE.name, VARIABLE.className);
}
System.Console.ReadLine();
}