linq与目录和数据库的简单实用
申明数据库字段
public class db { public string DirectoryName { get; set; } public string DirectoryInformation { get; set; } }
使用linq
List<db> dbList = new List<db>() { new db { DirectoryName = "Repositories", DirectoryInformation = "呵呵" }, new db { DirectoryName = "Intel", DirectoryInformation = "呵呵444" }, }; DirectoryInfo di = new DirectoryInfo("C:\\"); var query = from dir in di.GetDirectories() orderby di.Name select new { dir.Name, DirectoryDescription = (from d in dbList where d.DirectoryName == dir.Name select d.DirectoryInformation).FirstOrDefault() }; foreach (var item in query) { Console.WriteLine("{0}--{1}", item.Name, item.DirectoryDescription); } Console.ReadLine();
输出结果
//$360Section-- //$Recycle.Bin-- //360SANDBOX-- //360安全浏览器下载-- //Boot-- //Documents and Settings-- //dosh-- //fdlint-master-- //inetpub-- //Intel--呵呵444 //MSOCache-- //PerfLogs-- //Program Files-- //Program Files (x86)-- //ProgramData-- //Recovery-- //RECYCLER-- //Repositories--呵呵 //Ruby193-- //System Volume Information-- //Users-- //Windows--
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。