linq学习笔记(一)
来源:互联网 发布:我们相遇网络歌词 编辑:程序博客网 时间:2024/05/20 03:05
本例演示了如何从一个int数组中找出偶数,并将结果从大小到排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqDemo
{
class Program
{
static int[] numbers = { 1, 3, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };
static void Main(string[] args)
{
Traditonal();
Console.WriteLine("----------------------");
LinqMethod();
Console.ReadLine();
}
/**//// <summary>
/// 传统写法
/// </summary>
static void Traditonal()
{
List<int> SelectedNumbers = new List<int>();
foreach (int i in numbers)
{
if (i % 2==0)
{
SelectedNumbers.Add(i);
}
}
SelectedNumbers.Sort(SortDesc); //.net1.0写法
for (int i = 0; i < SelectedNumbers.Count; i++)
{
Console.WriteLine(SelectedNumbers[i]);
}
}
/**//// <summary>
/// 逆顺排序(配合传统写法)
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>1(x大于y),0(x等于y),-1(x小于y)</returns>
static int SortDesc(int x,int y)
{
//if (x < y)
//{
// return 1;
//}
//else if (x == y)
//{
// return 0;
//}
//else
//{
// return -1;
//}//也可以简写为下面的一行
return y - x;
}
/**//// <summary>
/// Linq的写法
/// </summary>
static void LinqMethod()
{
var SelectedNumbers = from number in numbers where (number % 2 == 0) orderby number descending select number;
foreach (var i in SelectedNumbers)
{
Console.WriteLine(i);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqDemo
{
class Program
{
static int[] numbers = { 1, 3, 4, 5, 6, 7, 8, 9, 10, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 };
static void Main(string[] args)
{
Traditonal();
Console.WriteLine("----------------------");
LinqMethod();
Console.ReadLine();
}
/**//// <summary>
/// 传统写法
/// </summary>
static void Traditonal()
{
List<int> SelectedNumbers = new List<int>();
foreach (int i in numbers)
{
if (i % 2==0)
{
SelectedNumbers.Add(i);
}
}
SelectedNumbers.Sort(SortDesc); //.net1.0写法
for (int i = 0; i < SelectedNumbers.Count; i++)
{
Console.WriteLine(SelectedNumbers[i]);
}
}
/**//// <summary>
/// 逆顺排序(配合传统写法)
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>1(x大于y),0(x等于y),-1(x小于y)</returns>
static int SortDesc(int x,int y)
{
//if (x < y)
//{
// return 1;
//}
//else if (x == y)
//{
// return 0;
//}
//else
//{
// return -1;
//}//也可以简写为下面的一行
return y - x;
}
/**//// <summary>
/// Linq的写法
/// </summary>
static void LinqMethod()
{
var SelectedNumbers = from number in numbers where (number % 2 == 0) orderby number descending select number;
foreach (var i in SelectedNumbers)
{
Console.WriteLine(i);
}
}
}
}
可以看出用Linq写法,代码更简洁
- linq学习笔记(一)
- linq学习笔记(一)
- Linq学习笔记(一) -- 初识Linq
- LinQ学习笔记(一)
- LINQ学习笔记(一)—感受篇
- LINQ学习笔记(一)—感受篇
- Linq to DataSet 学习笔记一
- Linq学习一
- Linq 学习(一)
- LinQ的学习(一)
- LINQ学习(一)
- 学习笔记(一)-Linq To Sql&PhotoShop第二章
- 学习 linq to ADO.NET 实例笔记(一)
- LINQ to SQL学习笔记(一)——Where
- C#学习笔记(一):泛型、lambda表达式和linq
- LINQ 之查询基础学习笔记(一)
- linq学习笔记(二)
- Linq学习笔记(三)
- 手机也许是解决票贩子问题的一个好办法
- 泛型类/泛型委托示例
- 倪光南院士:现在年轻人不够踏实
- 内联函数与外联函数的区别
- 推荐几个Java开发者应该多去的技术网站
- linq学习笔记(一)
- [编译记录]关于编译器错误C2885...
- 二月西班牙亮相 GARMIN推出首款3G touch screen GPS手机
- 世界编程语言排行榜08年01月_VB 语言
- CreateThread和_beginthread的区别
- const用法小结(转)
- 世界编程语言排行榜08年01月_PHP 语言
- 关于项目管理的知识点
- 定义类