linq 学习日记-select
引用地址:http://www.cnblogs.com/young18/archive/2007/06/05/772393.html
using
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
#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
#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
#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 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#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
#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
#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
#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 学生类
class Student
{
public int Age { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
#endregion
结果显示:
长期招聘java,有找工作可以联系我,微信:caozhenhua1563