LinQ的第一天(老规矩:还是案例分析,从简单到难)

LINQ-TO-SQL:
案例1:基础模型

static void Main(string[] args)
{
int[] int_array = { 1, 8, 5, 9, 15, 20 };

var filtered = from value in int_array//注意是var,可以帮助我们减少很多麻烦
where value > 4
select value;

Console.WriteLine(filtered.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Int32]
foreach (int i in filtered)
{
Console.Write(i + "");//8 5 9 15 20
}
Console.WriteLine();


Console.WriteLine("The Next:");
double[] double_array={1.6,2.5,1.5,7.5};
IEnumerable<double> double_arrayQuery =//标准格式
from value in double_array
where value > 2
select value;
Console.WriteLine(double_arrayQuery.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Double]
foreach (double testQuery in double_arrayQuery)
{
Console.Write(testQuery+"");//2.5 7.5
}
}




案例2:

 static void Main(string[] args)
{
int[] int_array = { 1, 8, 5, 9, 15, 20 };

var filtered = from value in int_array//注意是var
where value > 4
orderby value//按默认的规则升序排列
select value;

Console.WriteLine(filtered.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Int32]
foreach (int i in filtered)
{
Console.Write(i + "");//5 8 9 15 20
}
Console.WriteLine();


Console.WriteLine("The Next:");
double[] double_array={1.6,2.5,1.5,7.5};
IEnumerable<double> double_arrayQuery =//标准格式
from value in double_array
where value > 2
orderby value descending//降序排列
select value;
Console.WriteLine(double_arrayQuery.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Double]
foreach (double testQuery in double_arrayQuery)//注意这里查询的是哪个变量,是SQL的变量
{
Console.Write(testQuery+"");//7.5 2.5
}



string[] names = { "Mr.w","Jack","Angrw"};
Console.WriteLine("The names is :");
foreach (string nametest in names)
{
Console.Write(nametest + "");
}
Console.WriteLine();

IEnumerable<string> names_query =
from name in names
orderby name
select name;
Console.WriteLine("The Orderby of name is :");
foreach (string nametest in names_query)
{
Console.Write(nametest + "");
}
Console.WriteLine();

names_query =
from name in names
orderby name descending
select name;
Console.WriteLine("The Orderby...Descending of name is :");
foreach (string nametest in names_query)
{
Console.Write(nametest + "");
}
Console.WriteLine();
}
The Result:
/*
System.Linq.OrderedEnumerable`2[System.Int32,System.Int32]
5 8 9 15 20
The Next:
System.Linq.OrderedEnumerable`2[System.Double,System.Double]
7.5 2.5 The names is :
Mr.w Jack Angrw
The Orderby of name is :
Angrw Jack Mr.w
The Orderby...Descending of name is :
Mr.w Jack Angrw
*/





案例3:简化上面的代码

static void Main(string[] args)
{
int[] int_array = { 1, 8, 5, 9, 15, 20 };
var filtered = from value in int_array//注意是var
where value > 4
orderby value//按默认的规则升序排列
select value;
Console.WriteLine(filtered.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Int32]
Display(filtered, "Int");

double[] double_array={1.6,2.5,1.5,7.5};
IEnumerable<double> double_arrayQuery =//标准格式
from value in double_array
where value > 2
orderby value descending//降序排列
select value;
Console.WriteLine(double_arrayQuery.GetType());//System.Linq.Enumerable+<WhereIterator>d__0`1[System.Double]
Display(double_arrayQuery, "Double");


string[] names = { "Mr.w","Jack","Angrw"};
IEnumerable<string> names_query =
from name in names
orderby name
select name;
Display(names_query,"String-orderby");

names_query =
from name in names
orderby name descending
select name;
Display(names_query, "String-orderby..descending");
}
//为了简化我们写成方法调用
static void Display(IEnumerable<string> string_query, string welcome) //下面3个方法起到了方法重载作用
{
Console.WriteLine("The {0} is :", welcome);
foreach (string test in string_query)
{
Console.Write(test + "");
}
Console.WriteLine();
}

static void Display(IEnumerable<int> int_query, string welcome)
{
Console.WriteLine("The {0} is :", welcome);
foreach (int test in int_query)
{
Console.Write(test + "");
}

Console.WriteLine();
}

static void Display(IEnumerable<double> double_query, string welcome)
{
Console.WriteLine("The {0} is :", welcome);
foreach (double test in double_query)
{
Console.Write(test + "");
}

Console.WriteLine();
}
The Result:
/*
System.Linq.OrderedEnumerable`2[System.Int32,System.Int32]
The Int is :
5  8  9  15  20
System.Linq.OrderedEnumerable`2[System.Double,System.Double]
The Double is :
7.5  2.5
The String-orderby is :
Angrw  Jack  Mr.w
The String-orderby...descending is :
Mr.w  Jack  Angrw
*/


案例4:
 

class Program
{
static void Main(string[] args)
{

Member[] persons ={
new Member(-10m, 21, "jack"),
new Member(25m,25,"An"),
new Member(100m,18,"Mr.w"),
new Member(3000m,27,"deve")
};

Display(persons,"The Array");

var betwtenn100M3000M =
from e in persons
where e.Pay >= 100m && e.Pay <= 3000m
select e;
Display(betwtenn100M3000M, "100m-300m");

var PaySort =
from e in persons
orderby e.Pay //默认是月薪从小到大
select e;
Display(PaySort,"The Pay Sort");

var AgeSort =
from e in persons
orderby e.age descending //年龄从大到小
select e;
Display(AgeSort, "The Age Sort");
}

static void Display<T>(IEnumerable<T> T_query, string header)//泛型接口
{
Console.WriteLine("{0} :", header);

foreach (T test in T_query)
{
Console.WriteLine(test + "");
}
Console.WriteLine();
}
}

class Member//成员
{
private decimal pay;// 月薪
public decimal Pay//月薪属性
{
get { return pay; }
set
{
if (value >= 0m)
{
pay = value;
}

}
}
public string name;//这里如果不是public 外部将无法访问
public int age;

public Member(decimal monthPay, int member_age, string member_name)
{
name = member_name;
age = member_age;
Pay = monthPay;//注意这里的大写Pay
}

public override string ToString()//重写string类的ToString方法
{
return string.Format("Name is: {0} Age is:{1} MonthPay is:{2}",name,age,pay);

}

}
The Result:
/*
The Array :
Name is: jack Age is:21 MonthPay is:0
Name is: An Age is:25 MonthPay is:25
Name is: Mr.w Age is:18 MonthPay is:100
Name is: deve Age is:27 MonthPay is:3000

100m-300m :
Name is: Mr.w Age is:18 MonthPay is:100
Name is: deve Age is:27 MonthPay is:3000

The Pay Sort :
Name is: jack Age is:21 MonthPay is:0
Name is: An Age is:25 MonthPay is:25
Name is: Mr.w Age is:18 MonthPay is:100
Name is: deve Age is:27 MonthPay is:3000

The Age Sort :
Name is: deve Age is:27 MonthPay is:3000
Name is: An Age is:25 MonthPay is:25
Name is: jack Age is:21 MonthPay is:0
Name is: Mr.w Age is:18 MonthPay is:100
*/



posted @ 2011-12-16 16:58  Anleb  阅读(988)  评论(0编辑  收藏  举报