本帖最后由 徐赵华 于 2012-10-11 23:42 编辑
LINQ查询可以返回两种类型的结果------- 一个枚举,注:可枚举的一组数据,不是枚举类型。一个叫做标量的单一值。
int[] intArray = new int[] { 10, 3, 4, 16, 78, 14 };
string[] strArray = new string[] { "hello world", "doomsday", "china" };
IEnumerable<int> result = from n in intArray //查询语法
where n < 20
select n;
foreach (var x in result)
{
Console.WriteLine("{0}", x);
}
var nums = intArray.Where(x => x < 20); //方法语法
int intCount = (from n in intArray //两种形式的组合
where n <= 20
select n).Count();
IEnumerable<string> strResult = from n in strArray
where n == "hello world" || n == "doomsday"
select n;
foreach (string y in strResult)
{
Console.WriteLine(y);
}
|