var y = from model in list.Where(s=>s.product==product||product=="")
select new { sn = model.sn };
==========>>
var y = from model in list
where (monthly == "" || m.monthly == monthly) && (product == "" || model.product == product) && (factory == "" || model.factory == factory)
select new { sn = model.sn };
1.简单用法
说明:当以select结尾时表示的只是一个声明或者一个描述,并没有真正把数据取出来,只有当你需要该数据的时候,它才会执行这个语句,这就是延迟加载(deferred loading)。
var query = from student in students select student.Name;
foreach (var student in query) {
Console.WriteLine("{0}", student);
}
2.匿名类型形式
说明:其实质是编译器根据我们自定义产生一个匿名的类来帮助我们实现临时变量的储存。例如 var ob = new {Name = "Harry"},编译器自动产生一个有property叫做Name的匿名类,然后按这个类型分配内存,并初始化对象。
查询学生的姓名:
var query = from student in students
select new { newName = "学生姓名:" + student.Name };
foreach (var student in query) {
Console.WriteLine(student.newName);
}
3.条件形式
说明:三元运算,类似于SQL语句case when condition then else的用法。
查询学生的分数等级:
var query = from student in students
select new { student.Name, level = student.Score < 60 ? "不及格" : "合格" };
foreach (var student in query) {
Console.WriteLine("{0}:{1}", student.Name, student.level); //Terry:不及格 //AI:及格 //AI:及格
}
4.筛选形式
说明:结合where用起到过滤的作用。
var query = from student in students
where student.Name == "Terry"
select student;
5.嵌套类型形式
说明:如果一个数据源里面又包含了一个或多个集合列表,那么应该使用复合的select子句来进行查询。
class Student {
public string Name { get; set; }
public List<int> Scores { get; set; }
}
List<Student> students = new List<Student>{
new Student {Name="Terry", Scores=new List<int> {97, 72, 81, 60}},
new Student {Name="AI", Scores=new List<int> {75, 84, 91, 39}},
new Student {Name="Wade", Scores=new List<int> {88, 94, 65, 85}},
new Student {Name="Tracy", Scores=new List<int>{97, 89, 85, 82}},
new Student {Name="Kobe", Scores=new List<int> {35, 72, 91, 70}}
};
var query = from student in students
select new { student.Name, //生成新的集合对象
highScore=from sc in student.Scores
where sc>80
select sc
};
foreach (var student in query) {
Console.Write("{0}:",student.Name);
foreach (var scores in student.highScore) { Console.Write("{0},",scores); }
Console.WriteLine();
}
6.本地方法调用形式
var query = from student in students
select new
{
student.Name,
//调用GetLevel方法
level = GetLevel(student.Score)
};
foreach (var student in query)
{
Console.WriteLine("{0}:{1}", student.Name, student.level);
}
protected static string GetLevel(int score)
{
if (score > 60)
{
return "及格";
}
else
{
return "不及格";
}
}
7.Distinct形式
var query = (from student in students
select student.Name).Distinct();
foreach (var student in query)
{
Console.WriteLine("{0}", student);
//Terry:
//AI
}
8.OrderBy
var query = from student in students
orderby student.Score descending, student.Name
select student;
9.Group By
(1)说明:group子句返回一个 IGrouping<TKey, TElement> 对象序列,这些对象包含零个或更多个与该组的键值匹配的项。 例如,可以按照每个字符串中的第一个字母对字符串序列进行分组。 在这种情况下,第一个字母是键且具有 char 类型,并且存储在每个 IGrouping<TKey, TElement> 对象的Key属性中。
var query = from student in students
group student by student.Score;
foreach (var studentGroup in query)
{
//studentGroup推断为IGrouping<int,Student>类型
Console.WriteLine("{0}", studentGroup.Key);
}
(2)说明:由于 group 查询产生的 IGrouping<TKey, TElement> 对象实质上是列表的列表,因此必须使用嵌套的 foreach 循环来访问每一组中的各个项。 外部循环用于循环访问组键,内部循环用于循环访问组本身中的每个项。组可能具有键,但没有元素。如果您想要对每个组执行附加查询操作,则可以使用 into 上下文关键字指定一个临时标识符。 使用 into 时,必须继续编写该查询,并最终用一个 select 语句或另一个 group 子句结束该查询。
var query = from student in students
group student by student.Score into g
select g;
foreach (var studentGroup in query)
{
Console.WriteLine("分数组:{0}", studentGroup.Key);
foreach (var student in studentGroup)
{
Console.Write("{0}:{1},", student.Name,student.Score);
}
Console.WriteLine();
}
10.内部联接
说明:"内部联接"产生一个结果集,对于该结果集内第一个集合中的每个元素,只要在第二个集合中存在一个匹配元素,该元素就会出现一次。 如果第一个集合中的某个元素没有匹配元素,则它不会出现在结果集内。
查询学生的选课信息:
var query = from student in students
join cour in cours on student.Id equals cour.CourseId
select new
{
Id = student.Id,
Name = student.Name,
CourName = cour.CourseName
};
foreach (var student in query)
{
Console.WriteLine("{0} {1} {2}", student.Id, student.Name, student.CourName);
}
11.分组联接
说明:含有 into 表达式的 join 子句称为分组联接。分组联接本质上是一个对象数组序列,结果序列会组织为多个组形式数据进行返回就是会产生一个分层的结果序列。通俗点讲此序列第一个集合中的每个元素与第二个集合中的一组相关元素进行配对,如果找不到就返回空数组。我的理解是分组联接和内部联接基本差不多。
查询学生的选课信息:
var query = from student in students
join cour in cours on student.Id equals cour.CourseId into courseGroup
select new
{
Id = student.Id,
Name = student.Name,
CourInfo = courseGroup
};
foreach (var student in query)
{
Console.Write("{0} {1} ",student.Id,student.Name);
foreach (var cour in student.CourInfo)
{
Console.Write("{0}", cour.CourseName);
}
Console.WriteLine();
//C03 Terry PHP应用开发
//C03 James PHP应用开发
//C01 Kobe C#课程设计
//C02 AI Java深入
//C01 Wade C#课程设计
//C05 Kelly
//注意:C05没有匹配,但是返回空的数组
}
12.左外部联接
说明:在左外部联接中,将返回左侧源序列中的所有元素,即使它们在右侧序列中没有匹配的元素也是如此。 可以使用 LINQ,通过对分组联接的结果调用 DefaultIfEmpty 来执行左外部联接。
查询学生的选课信息:
var query = from student in students
join cour in cours on student.Id equals cour.CourseId into courseGroup
from stucour in courseGroup.DefaultIfEmpty()
select new
{
Id=student.Id,
Name = student.Name,
Cour = courseGroup
};
foreach (var student in query)
{
Console.Write("{0} {1} ", student.Id, student.Name);
foreach (var cour in student.Cour)
{
Console.Write("{0}",cour.CourseName);
}
Console.WriteLine();
}
13.聚合函数
说明:函数有很多,这里只介绍几个:Count、Max、Average、First。
查询学生的人数:
int count = (from student in students select student.NumId).Count(); Console.WriteLine("学生的人数:{0}", count); //学生的人数:6
查询最高分:
int maxscore = (from student in students select student.Score).Max(); Console.WriteLine("最高分:{0}", maxscore); //最高分:90
查询平均分:
double average = (from student in students select student.Score).Average(); Console.WriteLine("平均分:{0}", average); //平均分:68.3333333333333
返回列表第一个学生姓名:
string Name = (from student in students select student.Name).First(); Console.WriteLine("返回列表第一个学生姓名:{0}", Name);
14.生成结果集
说明:只有使用ToList 或 ToArray 等方法来强制转换,才能使变量立即获取结果集。
转换为List集合:
var querylist = (from student in students
select student).ToList();
foreach (var student in querylist)
{
Console.WriteLine("{0} {1} {2}", student.NumId, student.Name, student.Score);
//3 Terry 55
//1 AI 80
//3 Kobe 40
//8 James 90
//5 Love 60
//6 Wade 85
//注意:变量querylist已经存储结果集
}
转换为Array数组:
var queryarray = (from student in students
select student).ToArray();
foreach (var student in queryarray)
{
Console.WriteLine("{0} {1} {2}", student.NumId, student.Name, student.Score);
//3 Terry 55
//1 AI 80
//3 Kobe 40
//8 James 90
//5 Love 60
//6 Wade 85
//注意:变量queryarray已经存储结果集
}
http://www.cnblogs.com/ForEvErNoME/archive/2012/07/25/2606659.html
http://www.cnblogs.com/ForEvErNoME/archive/2012/08/21/2633767.html