LINQ 常用from

Posted 天高任我飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINQ 常用from相关的知识,希望对你有一定的参考价值。

单个form子句
string[] values = { "LINQ学习", "LINQ基本语句", "from子句", "单个from子句" };
var value = from v in values
where v.IndexOf("LINQ") > -1 //包含字符串
select new { v, v.Length };//返回自定义数组
foreach (var n in value)
{
Response.Write(n.v+"###"+n.Length);
}


使用LINQ查询ArrayList
ArrayList gList = new ArrayList();
gList.Add(new GustInfo { Name="DebugLZQ", Age=26, Tel="88888888"});
gList.Add(new GustInfo { Name="博客园",Age=6, Tel ="666666"});
gList.Add(new GustInfo { Name = "M&MSoft", Age =9, Tel = "55555" });

var query = from GustInfo gust in gList
where gust.Age > 9
select gust;//范围变量gust制定了数据类型
foreach (GustInfo g in query)
{
Console.WriteLine("{0} 年龄:{1} 电话:{2}",g.Name,g.Age,g.Tel );
}

//复合from子句
List<GustInfo1> gList2 = new List<GustInfo1>()
{
new GustInfo1{ Name="DebugLZQ",Age=26,TelTable=new List<string>(){"8888888","138******"}},
new GustInfo1{ Name="博客园",Age=6,TelTable =new List<string>(){"666666","138******"}},
new GustInfo1{ Name="M&MSoft",Age=9,TelTable=new List<string>(){"55555","138******"}}
};

//gust、tel都是查询变量,作用域为当前查询语句!!!
var query2 = from gust in gList2
from tel in gust.TelTable
where tel.IndexOf("8888888") > -1
select gust;
foreach (var g in query2)
{
Response.Write(g.Name+" 年龄"+ g.Age);
foreach (var t in g.TelTable)
{
Response.Write("电话:"+t);
}
}

 

//4多个from子句
ArrayList gList = new ArrayList();
gList.Add(new GustInfo { Name = "DebugLZQ", Age = 26, Tel = "88888888" });
gList.Add(new GustInfo { Name = "博客园", Age = 6, Tel = "666666" });
gList.Add(new GustInfo { Name = "M&MSoft", Age = 9, Tel = "55555" });
List<GustInfo1> gList2 = new List<GustInfo1>()
{
new GustInfo1{ Name="DebugLZQ",Age=26,TelTable=new List<string>(){"8888888","138******"}},
new GustInfo1{ Name="博客园",Age=6,TelTable =new List<string>(){"666666","138******"}},
new GustInfo1{ Name="M&MSoft",Age=9,TelTable=new List<string>(){"55555","138******"}}
};
//4多个from子句
var query3 = from GustInfo gust in gList
where gust.Age > 6
from GustInfo1 gust2 in gList2
where gust2.Age > 9
select new { gust, gust2 };//查询结果定制
foreach (var g in query3)
{
Response.Write(g.gust.Name + "###" + g.gust2.Name);
}

以上是关于LINQ 常用from的主要内容,如果未能解决你的问题,请参考以下文章

Linq To Sql常用方法使用总结

常用linq语法 - 孔小爽 - 博客园

LinQ常用高级查询

linq to entity常用操作

List中常用的linq操作

常用LINQ关键字用法汇总