using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ_WHERE
{
/**//// <summary>
/// LINQ where子句
/// </summary>
class Program
{
static void Main(string[] args)
{
//1常见的where语句
List<GustInfo> gList = new List<GustInfo>()
{
new GustInfo(){ Name="DebugLZQ", Age=26,Tel="88888888"},
new GustInfo(){ Name="cnblogs",Age=6,Tel="666666"},
new GustInfo(){ Name="M&MSoft",Age=9,Tel="55555"}
};
var query = from gust in gList
where (gust.Name.Length > 7 || gust.Name.Substring(0, 1) == "M") && gust.Age > 9
select new { gust.Name, gust.Age };
foreach (var g in query)
{
Console.WriteLine("{0},{1}", g.Name, g.Age);
}
Console.ReadKey(false);
//2.在where子句中使用自定义函数
var query2 = from GustInfo gust in gList
where gust.Name.Length > 5
&& Check(gust.Name)
select gust;
foreach (var g in query2)
{
Console.WriteLine("{0},{1},{2}", g.Name, g.Age, g.Tel);
}
Console.ReadKey(false);
//3.动态谓词的筛选
//定义动态谓词数组,在实际开发中可以动态获得
string[] names = { "SB","XXX","***","@@@","一些敏感词"};
var query3 = from GustInfo guest in gList
where !names.Contains(guest.Name)
select guest;
foreach (var q in query3)
{
Console.WriteLine("{0} 年龄:{1},电话:{2}",q.Name,q.Age,q.Tel );
}
Console.ReadKey(false);
}
//自定义函数
static bool Check(string name)
{
if (name.Substring(0, 1) == "N")
return false;
return true;
}
}
}
文章转载自: Linq中where查询 http://www.studyofnet.com/news/269.html