需要注意一些常用的where子句的写法。程序的运行结果如下:
3.Select子句
在select子句上可以非常灵活的处理查询到的元素,然后再把结果返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ_select
{
/// <summary>
/// LINQ select
/// 在select子句上,可以非常灵活的处理查询到的元素,然后再把结果返回
/// </summary>
class MyGustInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<GustInfo> gList = new List<GustInfo>()
{
new GustInfo(){ Name="DebugLZQ", Age=25, Tel="88888888"},
new GustInfo(){ Name="cnblogs", Age=6, Tel="666666"},
new GustInfo(){ Name="M&M", Age=9, Tel="55555"}
};
var query = from gust in gList
where gust.Age >= 9 && gust.Age <= 30
select gust.Name.Replace("&", "mm");//select子句灵活应用
var query2 = from gust in gList
where gust.Age >= 9 && gust.Age <= 30
select MyProc(gust.Name);
var query3 = from gust in gList
where gust.Age >= 9 && gust.Age <= 30
select new { gust.Name,gust.Age};
var query4 = from gust in gList
where gust.Age >= 9 && gust.Age <= 30
select new MyGustInfo { Name=gust.Name+"My", Age=gust.Age+1};//对查询结果进行投影
foreach (var v in query)
{
Console.WriteLine(v);
}
foreach (var v in query2)
{
Console.WriteLine(v);
}
foreach (var v in query3)
{
Console.WriteLine(v.Name+v.Age );
}
foreach (var v in query4)
{
Console.WriteLine(v.Name+v.Age );
}
Console.ReadKey(false);
}
static string MyProc(string s)
{
return s + "Better";
}
}
}
程序的运行结果如下:











