?!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
本文׃软g公司介绍Linq查询基本操作(查询关键?
- from 子句
- where 子句
- select子句
- group 子句
- into 子句
- orderby 子句
- join 子句
- let 子句
- 复合from子句
- 在某些情况下Q源序列中的每个元素本n可能是序?集合),也可能包含序?/p>
- 用语讉K单个数据库中的内部集?/p>
- 使用多个from字句执行q接
- 可以包含多个可从独立数据源生成补充查询的from字句
复合(思义是有多from的字?实例Q?/p>
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student
{
LastName="xiaogui",Scores=new List<int>{97,42,91,60}},
new Student
{
LastName="xiaozhan",Scores=new List<int>{50,92,81,60}},
new Student
{
LastName="xiaolan",Scores=new List<int>{32,32,81,90}},
new Student
{
LastName="xiaowan",Scores=new List<int>{92,22,81,60}},
};
var query = from stuent in students
from score in stuent.Scores
where score > 90
select new
{
Last = stuent.LastName,
score
};
foreach (var student in query)//大于90分的昄出来
{
Console.WriteLine("{0} Score:{1}", student.Last, student.score);
}
Console.ReadLine();
}
}
public class Student
{
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
public class Employee
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
}
执行l果: xiaogui Score:97
xiaogui Score:91
xiaozhan Score:92
xiaowan Score:92
let 关键?使用let字句扩展范围变量)
- 创徏一个可以查询自w的可枚丄?/p>
- 使查询只能对范围变量word调用一ơToLower?/p>
如果不用letQ则必须在where字句的每个谓词中调用ToLower
例:把每个单词开头包含a或者e的找出来
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
string[] strings = { "A penny saved is a penny earned.", "The aaxly sdj", "the pa is no" };
var query = from sentence in strings
let words = sentence.Split(' ')//用空格分割成数组
from word in words
let w = word.ToLower()//把每个字母小?
where w[0] == 'a' || w[0] == 'e'
select word;
foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
where 关键?Q筛选)
- 一个查询表辑ּ可以包含多个where字句
例:(把包含a的找出来)
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
string[] str = { "a", "b", "c" };
var query = from s in str
where s == "a"
select s;
foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
orderby 关键?(排序)
- 在查询表辑ּ中,orderby字句可以使返回的序列(l?按升序或降序?/p>
- 可以指定多个键,以便执行一个或多个ơ要排序操作
- 默认排序序为升?/p>
- ~译Ӟorderby字句被{换ؓ对OrderByҎ的调用。orderby字句中的多个键被转换为ThenByҎ调用
descending 降序
ascending 升序
?Q升?/strong>
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
string[] str = { "a", "b", "c" };
var query = from s in str
orderby s ascending
select s;
}
}
l果? a b c
?Q降?/p>
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
string[] str = { "a", "b", "c" };
var query = from s in str
orderby s descending
select s;
}
}
l果? c b a
group 关键?(分组)
- group 字句q回一个IGrouping(TKey,Telement)对象序列
- ~译Ӟgroup子句被{换ؓ对GroupByҎ的调?/p>
(LINQ查询表达式可以以select或者Groupl束) (如果惌Ҏ个组执行附加查询操作Q则可以使用into上下文关键字指定一个时标识符Q用intoӞ必须l箋~写该查询,q最l用一个select语句或另一 个group子句l束该查?
?
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
string[] str = { "aa", "bb", "cc", "dd" };
var query = from s in str
group s by s[0]
into p
where p.Key == 'a' || p.Key == 'b' || p.Key == 'c'
orderby p.Key descending
select p.Key;
foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
l果? c b a
说明Q?group s by s[0] into p 意思是针对范围变量s用“s[0]”分l,本例中是用第一个字母分l?/p>
join 关键?(联接)
- 使用join 子句可以来自不同源序列q且在对象模型中没有直接关系的元素相兌
- 唯一的要求是每个源中的元素需要共享某个可以进行比较以判断是否相等的?/p>
- join字句使用Ҏ的equals关键字比较指定的键是否相{?/p>
三中常见的连接类?/p>
- 内部联接
- 分组联接
- 左外部联?/p>
1.内部联接
var query = from a
in str
join b in str2 on a.id equals b.id
select new { Aname = a.name, Bname = b.name };
2.分组联接Q?/strong>Qinto 可以join暂存hQ?/p> var query = from a in str join b in str2 on a.id equals b.id into c select new {Aname=a.name,Cs=c} 3.左外部联?/strong> - 在左外部联接中,返回左则源序列中所有元素,即它们在右则序列中没有匚w的元素也是如此?/p> - 若要在LINQ中执行左外部联接Q请DefaultifEmptyҎ与分l联接结合v来,以指定要在某个左则元素不h匚w元素时生的默认叛_元素。可以用null作ؓM?/p> 用类型的默认|也可以指定用户定义的默认cd?/p> var query = from category in categories join prod in products on category.id equals prod.CategoryID into prodGroup from item prodGroup.defaultifEmpty( new Product{Name=string.empty,CategoryID=0}) select new {CatName=category.Name,ProdName=item.Name} equalse q算W?/p> - join 子句执行同等联接。换句话_只能Z两个键之间的相等关系q行匚w - Z表明所有联接都是相{联接,join子句使用equals关键字而不?=q算W?/p>