前言
简单介绍一下linq 查询表达式。
正文
上文其实已经介绍了查询表达式了。
但是呢,这里就介绍一些复杂一点的。
这里不会去介绍查询表达式,而是直接介绍一些复杂的。
let 字句。
static void Main(string[] args)
{
var files = from fileName in Directory.EnumerateFiles("c://", "test")
orderby new FileInfo(fileName).LastAccessTime, fileName descending
select new FileInfo(fileName);
}
这样写会new FileInfo两遍,如果使用let 那么就是一遍。
static void Main(string[] args)
{
var files = from fileName in Directory.EnumerateFiles("c://", "test")
let file = new FileInfo(fileName)
orderby file.LastAccessTime, fileName descending
select file;
}
第二个是into:
连续查询。
就是一个查询字句,作为第二个查询的集合,像下面这样:
static void Main(string[] args)
{
var files = from fileName in Directory.EnumerateFiles("c://", "test")
let file = new FileInfo(fileName)
orderby file.LastAccessTime, fileName descending
select file into temp
where temp.FullName != "123"
select temp;
}
前面文章中,使用selectMany,如果用linq 语句如何写呢?
static void Main(string[] args)
{
List<BasketballTeam> list = new List<BasketballTeam>();
BasketballTeam basketballTeam = new BasketballTeam();
basketballTeam.Name = "无敌球队";
basketballTeam.TeamMember = new string[] { "张三", "李四", "王五" };
BasketballTeam basketballTeam1 = new BasketballTeam();
basketballTeam1.Name = "小新球团";
basketballTeam1.TeamMember = new string[] { "张嘛子", "李老帽", "王七三" };
list.Add(basketballTeam);
list.Add(basketballTeam1);
var test = from team in list
from member in team.TeamMember
select team.Name + " "+ member;
Console.ReadKey();
}
使用两个from处理子集的情况。
结
linq 基础篇完成,后续为集合篇或者异步篇。
标签:string,c#,linq,fileName,file,三十,new,select From: https://www.cnblogs.com/aoximin/p/16989521.html