Linq学习笔记(1.2)——select
最近学习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>格式化化查询结果:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = from n in stringArray
where n.Length >= 6
select "I'm studying " + n;
foreach (var book in books){
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
只查询合适对象集合的某个属性#region 只查询合适对象集合的某个属性
Response.Write("<hr>只查询合适对象集合的某个属性:<br>");
List<Student> StudentList = GetStudents();
var students1 = from n in StudentList
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>从一个集合中查询,转化到另外一个集合:<br>");
int[] intArray = { 3, 1, 6, 4 };
var students2 = from n in intArray
select StudentList[n];
foreach (var student in students2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", student.Name));
}
#endregion
构建匿名类型#region 构建匿名类型
Response.Write("<hr>构建匿名类型:<br>");
var stu = from n in intArray
select new { Id = n, Name = StudentList[n].Name };
foreach (var s in stu)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}</div>", s.Id,s.Name));
}
#endregion
构建匿名类型2——扩展已有类#region 构建匿名类型2——扩展已有类
Response.Write("<hr>构建匿名类型2——扩展已有类:<br>");
var students3 = from n in intArray
select new { Id = n, StudentList[n].Name, StudentList[n].Age };
foreach (var student in students3)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}; age:{2}</div>", student.Id, student.Name, student.Age));
}
#endregion
select的Lambda表达式#region select的Lambda表达式
Response.Write("<hr>select的Lambda表达式:<br>");
var students4 = intArray.Select(i => new { Id = i, StudentList[i].Name });
foreach (var student in students4)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}</div>", student.Id, student.Name));
}
#endregion
复合选择#region 复合选择
Response.Write("<hr>复合选择:<br>");
var study4 = from a in StudentList where a.Age>23
from b in stringArray where b.Length>8
select new {name=a.Name,book=b};
foreach (var student in study4)
{
Response.Write(string.Format("<div class='result'>{0} is studying {1}</div>", student.name, student.book));
}
#endregion
多重选择#region 多重选择
Response.Write("<hr>多重选择:<br>");
var study =
from a in StudentList
where a.Age > 20
from b in stringArray
where b.Length > 5
select new {a.Name,book=b};
foreach (var s in study)
{
Response.Write(string.Format("<div class='result'>name:{0}; book:{1}</div>", s.Name, s.book));
}
#endregion
多重选择的Lambda表达式#region 多重选择的Lambda表达式
Response.Write("<hr>多重选择的Lambda表达式:<br>");
var study1 = StudentList.SelectMany(student => stringArray.Select(str => student.Name + " has a book names " + str));
foreach (var s in study1)
{
Response.Write(string.Format("<div class='result'>{0}</div>", s));
}
#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>格式化化查询结果:<br>");
string[] stringArray = { "Ajax", "Csharp", "Javascript", "Css Desige", "Asp.net", "Gis" };
var books = from n in stringArray
where n.Length >= 6
select "I'm studying " + n;
foreach (var book in books){
Response.Write(string.Format("<div class='result'>{0}</div>", book));
}
#endregion
只查询合适对象集合的某个属性#region 只查询合适对象集合的某个属性
Response.Write("<hr>只查询合适对象集合的某个属性:<br>");
List<Student> StudentList = GetStudents();
var students1 = from n in StudentList
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>从一个集合中查询,转化到另外一个集合:<br>");
int[] intArray = { 3, 1, 6, 4 };
var students2 = from n in intArray
select StudentList[n];
foreach (var student in students2)
{
Response.Write(string.Format("<span class='result'>{0}</span>", student.Name));
}
#endregion
构建匿名类型#region 构建匿名类型
Response.Write("<hr>构建匿名类型:<br>");
var stu = from n in intArray
select new { Id = n, Name = StudentList[n].Name };
foreach (var s in stu)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}</div>", s.Id,s.Name));
}
#endregion
构建匿名类型2——扩展已有类#region 构建匿名类型2——扩展已有类
Response.Write("<hr>构建匿名类型2——扩展已有类:<br>");
var students3 = from n in intArray
select new { Id = n, StudentList[n].Name, StudentList[n].Age };
foreach (var student in students3)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}; age:{2}</div>", student.Id, student.Name, student.Age));
}
#endregion
select的Lambda表达式#region select的Lambda表达式
Response.Write("<hr>select的Lambda表达式:<br>");
var students4 = intArray.Select(i => new { Id = i, StudentList[i].Name });
foreach (var student in students4)
{
Response.Write(string.Format("<div class='result'>id:{0}; name:{1}</div>", student.Id, student.Name));
}
#endregion
复合选择#region 复合选择
Response.Write("<hr>复合选择:<br>");
var study4 = from a in StudentList where a.Age>23
from b in stringArray where b.Length>8
select new {name=a.Name,book=b};
foreach (var student in study4)
{
Response.Write(string.Format("<div class='result'>{0} is studying {1}</div>", student.name, student.book));
}
#endregion
多重选择#region 多重选择
Response.Write("<hr>多重选择:<br>");
var study =
from a in StudentList
where a.Age > 20
from b in stringArray
where b.Length > 5
select new {a.Name,book=b};
foreach (var s in study)
{
Response.Write(string.Format("<div class='result'>name:{0}; book:{1}</div>", s.Name, s.book));
}
#endregion
多重选择的Lambda表达式#region 多重选择的Lambda表达式
Response.Write("<hr>多重选择的Lambda表达式:<br>");
var study1 = StudentList.SelectMany(student => stringArray.Select(str => student.Name + " has a book names " + str));
foreach (var s in study1)
{
Response.Write(string.Format("<div class='result'>{0}</div>", s));
}
#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
结果显示: