Linq学习笔记(1.1)——where
最近学习Linq时练习的一些Demo
using#region using
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
根据条件查询数组#region 根据条件查询数组
Response.Write("<hr>列出数组中字符长度大于等于6的字符串数组:<br>");
string[] Demo1 = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = from n in Demo1
where n.Length >= 6
select n;
foreach (var book in books){
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
where查询的Lambda表达式#region where查询的Lambda表达式
Response.Write("<hr>where查询的快捷方法:<br>");
var books2 = Demo1.Where(book => book.Length >= 6);
foreach (var book in books2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
根据条件查询集合体#region 根据条件查询集合体
Response.Write("<hr>列出集合中语言为Chinese的学生:<br>");
List<Student> Demo2 = GetStudents();
var students1 = from n in Demo2
where n.Language == "Chinese"
select n.Name;
foreach (var student in students1){
Response.Write(string.Format("<span class='result'>{0}</span>", student));
}
#endregion
根据条件查询集合体#region 根据条件查询集合体
Response.Write("<hr>列出集合中年龄大于等于20的英国或苏格兰学生:<br>");
var students2 = from n in Demo2
where (n.Language == "English" || n.Language == "Scotland") && n.Age >= 20
select n;
foreach (var student in students2){
Response.Write(string.Format("<span class='result'>{0}</span>", student.Name));
}
#endregion
}
构造一个学生集合体#region 构造一个学生集合体
private List<Student> GetStudents()
{
List<Student> students = new List<Student> {
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
new Student{ Name="KELLY", Age=18, Language="English"},
new Student{ Name="JUNE", Age=20, Language="English"},
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
new Student{ Name="BRANT", Age=30, Language="Germany"},
new Student{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
}
#endregion
}
学生类#region 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#endregion
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
根据条件查询数组#region 根据条件查询数组
Response.Write("<hr>列出数组中字符长度大于等于6的字符串数组:<br>");
string[] Demo1 = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = from n in Demo1
where n.Length >= 6
select n;
foreach (var book in books){
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
where查询的Lambda表达式#region where查询的Lambda表达式
Response.Write("<hr>where查询的快捷方法:<br>");
var books2 = Demo1.Where(book => book.Length >= 6);
foreach (var book in books2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", book));
}
#endregion
根据条件查询集合体#region 根据条件查询集合体
Response.Write("<hr>列出集合中语言为Chinese的学生:<br>");
List<Student> Demo2 = GetStudents();
var students1 = from n in Demo2
where n.Language == "Chinese"
select n.Name;
foreach (var student in students1){
Response.Write(string.Format("<span class='result'>{0}</span>", student));
}
#endregion
根据条件查询集合体#region 根据条件查询集合体
Response.Write("<hr>列出集合中年龄大于等于20的英国或苏格兰学生:<br>");
var students2 = from n in Demo2
where (n.Language == "English" || n.Language == "Scotland") && n.Age >= 20
select n;
foreach (var student in students2){
Response.Write(string.Format("<span class='result'>{0}</span>", student.Name));
}
#endregion
}
构造一个学生集合体#region 构造一个学生集合体
private List<Student> GetStudents()
{
List<Student> students = new List<Student> {
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
new Student{ Name="KELLY", Age=18, Language="English"},
new Student{ Name="JUNE", Age=20, Language="English"},
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
new Student{ Name="BRANT", Age=30, Language="Germany"},
new Student{ Name="BEN", Age=25, Language="Chinese"}
};
return students;
}
#endregion
}
学生类#region 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#endregion
显示结果: