string[] names =
{"liuli","linhong","zhuming","sunyao","tieting","lixiaomei"
};
string[] Eng =
{"Tom","Dick","Harry","Mary","Jay","Lenda","Lili" };
private void button1_Click(object sender,
EventArgs e)
{
//标准查询运算
IEnumerable collectionZero = System.Linq.Enumerable.Where(names, r
=> r.Contains("王"));
IEnumerable collectionOne = names.Where(n => n.Length >=
3);
//表达式查询法
IEnumerable collectionTwo = from n in names
where
n.Contains("liu")
select
n;
IEnumerable collectionThree = Eng.Where(n => n.Length >=
3).OrderBy(n => n.Length).ThenBy(n => n).Select(n => n +
"-" + n);
IEnumerable collectionFour = from n in Eng
where n.Length >= 3
orderby n.Length
select n.ToUpper();
//查询运算符,where select selectManay thenby orderby
orderbyDescending thenbyDescending groupby join
groupJoin
//聚合函数 和
混合查询
int count
= (from n in Eng where n.Length > 3 select n).Count();
string
first = (from n in Eng where n.Length > 3 select
n).First();
//延迟加载或操作,但是改变后缀后 如
first,count,toarray,tolist,toDictionary可立即执行
//
LastForeach();//延迟加载
//子查询
IEnumerable collectionFive = from n in Eng
where n.Length == Eng.OrderBy(r =>
r.Length).First().Length
orderby n
select n;
//into关键字
创建一个新的一次新的查询
IEnumerable collectionSix = from n in Eng
select
n.ToUpper()
into n2
where n2.Length > 3
select n2;
//范围变量let
into from join
IEnumerable collectionSeven = from n in Eng
let BianLiang = n.Replace("a", "").Replace("e",
"").Replace("i", "").Replace("o", "").Replace("u", "")
where BianLiang.Length > 3
orderby BianLiang
select BianLiang;
ShowElements(collectionSeven);
//selectMany查询 主要对数据库的查询 -连接查询
IEnumerable collectionEight = from n1 in
Eng
from n2 in Eng
where n1.Equals(n2)//连接条件
select n1 + " - " + n2;
IEnumerable collectionNine = from n1 in names
from n2 in Eng
where n1.Length==n2.Length
select n1 + " - " + n2;
//Zip运算符拉锁
IEnumerable collectionTen = names.Zip(Eng, (n, m) => n + " = " +
m);
IEnumerable collectionEleven = Eng.OrderBy(n =>
n.Length).ThenBy(n=>n);
//ShowElements(collectionTen);
//groupby运算符
string[]
files = Directory.GetFiles("F:\\c#视频");
var query
= files.GroupBy(f => Path.GetExtension(f));
foreach
(IGrouping group in query)
{
listBox1.Items.Add("文件扩展名 :"
+ group.Key);
listBox1.Items.Add("");
foreach (string filename in
group)
{
listBox1.Items.Add("---- " + filename);
}
listBox1.Items.Add("");
}
}
public void ShowElements(IEnumerable
Elements)
{
listBox1.Items.Clear();
foreach
(string str in Elements)
{
listBox1.Items.Add(str);
}
}
private void LastForeach(ListBox listBox1)
{
listBox1.Items.Clear();
List list
= new List();
list.Add("toom");
list.Add("lili");
list.Add("Jay"); list.Add("lenda");
list.Add("xim");
IEnumerable collection = (from n in list
where n.Length >= 4
orderby n
select n);//.ToList();
list.Add("liulei");
foreach
(string str in collection)
{
listBox1.Items.Add(str);
}
}
private void button2_Click(object sender,
EventArgs e)
{
int[]
numbersA = { 0,2,4,5,6,8,9};
int[]
numbersB = { 1,3,5,7,8};
var query
= from a in numbersA
from b in
numbersB
where a
< b
select
new
{
a,
b
};
foreach
(var s in query)
{
listBox1.Items.Add(s);
}
}
加载中,请稍候......