Linq查询
2010-10-09 18:55 smat 阅读(180) 评论(0) 编辑 收藏 举报
一直认为,如果是代码相对比较简单的话,就不用说太多的话了!直接贴代码,更实际些,阅读者方便,笔者也方便LINQ查询的使用(笔记) - Ss_Andy - 温故而知新 可以为师矣
这篇笔记主要记录使用LINQ查询。
首先,我们先定义一个Car类,以供后面使用,具体代码如下:
public class Car
{
public string PetName = string.Empty;
public string Color = string.Empty;
public int Speed;
public string Make = string.Empty;
public override string ToString()
{
return string.Format("Make={0},Color={1},Speed = {2}, PetName={3}", Make, Color, Speed, PetName);
}
}
然后,在你的Main()方法里填充一下由下列 Car对象组成的数组,如下所示:
static void Main(string[] args)
{
Car[] myCars = new[]{
new Car{ PetName="Henry", Color="Silver", Speed=100, Make="BMW"},
new Car{ PetName="Daisy", Color="Tan", Speed=90, Make="BMW"},
new Car{ PetName="Mary", Color="Black", Speed=55, Make="VW"},
new Car{ PetName="Clunker", Color="Rust", Speed=5, Make="Yugo"},
new Car{ PetName="Hank", Color="Tan", Speed=0, Make="Ford"},
new Car{ PetName="Sven", Color="White", Speed=90, Make="Ford"},
new Car{ PetName="Mary", Color="Black", Speed=55, Make="VW"},
new Car{ PetName="Zippy", Color="Yellow", Speed=55, Make="VW"},
new Car{ PetName="Melvin", Color="White", Speed=43, Make="Ford"},
};
//我们将在这里调用各种方法!
Console.ReadLine();
}
好了,接下来我们就开始学习如何使用Linq进行查询吧!
基本的选择语法
因为LINQ查询表达式是在编译时校验的,记住这些运算符的次序是至关重要的。简单的说,每个LINQ查询表达式都是使用from,in和select运算建立起来的,如下所示.
var result = from item in container select item;
1.下面我们将编写一个最简单最简单的LINQ查询,就是查出所有对象,具体代码如下所示:
///
/// 查出所有数据
///
///
static void SelectAll(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -查出所有 - - - - - - - - - - - - - - - - -");
var allCars = from c in myCars select c;
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
}
读者可以在Main()方法里面添加SelectAll的调用,查看输出结果
(PS:不一定要查出整个对象,可以将修改上面的var allCars = from c in myCars select c;
var allCars = from c in myCars select c.PetName);
2.可根据条件筛选出想要的结果集,请看下面的代码:
///
/// 根据条件筛选
///
///
static void Where(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -根据条件筛选 - - - - - - - - - - - - - - - - -");
var allCars = from c in myCars where c.Speed > 50 select c;
//var allCars = from c in myCars where c.Speed > 50 && c.PetName == "Henry" select c;
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
}
3.可过滤掉重复的数据,请看下面的代码:
///
/// 过滤重复数据
///
///
static void Distinct(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -过滤重复数据 - - - - - - - - - - - - - - - - -");
var allCars = (from c in myCars select c.Make).Distinct();
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
}
4.可从现在有的数据源投影出新的数据形式,请看下面的代码:
///
/// 投影出新的数据类型
///
///
static void ManyProperty(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -投影出新的数据类型 - - - - - - - - - - - - - - - - -");
var allCars = from c in myCars select new { c.PetName, c.Speed };
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
}
5.也可以逆转结果集的顺序,请看下面的代码:
///
/// 逆转结果集的顺序
///
///
static void Reversed(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -逆转结果集的顺序 - - - - - - - - - - - - - - - - -");
var allCars = (from c in myCars select c).Reverse();
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
//也可以把Reverse用在foreach里面
//var allCars = from c in myCars select c;
//foreach (var c in allCars.Reverse())
//{
// Console.WriteLine(c.ToString());
//}
}
6.对表达式进行排序,请看下面的代码:
///
/// 对表达式进行排序
///
///
static void Orderby(Car[] myCars)
{
Console.WriteLine("- - - - - - - - - - - -对表达式进行排序 - - - - - - - - - - - - - - - - -");
var allCars = from c in myCars orderby c.PetName ascending select c; //升序
//var allCars = from c in myCars orderby c.PetName descending select c; //降序
foreach (var c in allCars)
{
Console.WriteLine(c.ToString());
}
}
7.找出两个结果集中不同的数据,请看下面代码:
///
/// 找出不同
///
static void GetDiff()
{
Console.WriteLine("- - - - - - - - - - - -找出不同 - - - - - - - - - - - - - - - - -");
List myCars = new List { "Yugo", "Aztec", "BMW" };
List YouCars = new List { "BMW", "Saab", "Aztec" };
var cardiff = (from c in myCars select c).Except(from c2 in YouCars select c2);
//var cardiff = (from c in myCars select c).Except(YouCars); //也可以这样写,和上一行代码同样的结果
foreach (string s in cardiff)
{
Console.WriteLine(s);
}
}
读者可以把在Main()方法里面调用这些方法,看看输出的结果...
以上这些示例足够让我们了解构建LINQ查询表达式的过程了。。
可能看到这里,会有读者发出疑问,是不是LINQ表达式返回的结果只能用var隐式类型来接收呢,当然不是!不过这个要看具体的情况。如果我们的结果集由强类型数据构成(诸如字符串数组,Car的List等),就可以不使用var关键字而使用合适的 Ienumerable或IEnumerable类型。这样说可能不够明显,还是用老方法,我们看代码说话,请看下面的代码:
static IEnumerable GetStringSubset()
{
string[] currentVideoGames = { "Morrowind", "BioShock", "Half Life 2: Episode 1",
"The Darkness", "Daxter", "System Shock 2" };
IEnumerable subset = from g in currentVideoGames where g.Length > 6 orderby g select g;
return subset;
}
由于方法的返回值不是返回隐式类型(var),那假如我们需要返回var类型的结果集,应如何呢?我们在得到var的结果集后调用ToArray()方法即可;
例 :
var subset = from g in currentVideoGames select g;
return subset.ToArray();