日期:2014-05-20  浏览次数:20751 次

linq语句出错求救
var list =from c in Directory.GetFiles(copyRarpath, "*.rar", SearchOption.TopDirectoryOnly)
  where c.Length > int.Parse(_serviceManage.RetentionBakFileLength)
  orderby DateTime.Parse(c.Replace(".rar", "").Replace("_", "-").Replace(bakrarPath, "")) descending
  select c;

这个貌似c in Directory.GetFiles(copyRarpath, "*.rar", SearchOption.TopDirectoryOnly)不正确
难道不能拿数组做成对象??
错误提示:表达式不能包含query expression

------解决方案--------------------
from c in Directory.GetFiles(copyRarpath, "*.rar", SearchOption.TopDirectoryOnly).Select((x, i) => new { x, i })
where c.i > int.Parse(_serviceManage.RetentionBakFileLength)
orderby DateTime.Parse(c.x.Replace(".rar", "").Replace("_", "-").Replace(bakrarPath, "")) descending
select c.x;


------解决方案--------------------
再仔细查查吧,你给的代码可以通过编译、执行的,如下:

C# code

            string copyRarpath = @"d:\temp";
            string bakrarPath = "bak";

            string RetentionBakFileLength = "18";

            var list = from c in Directory.GetFiles(copyRarpath, "*.rar", SearchOption.TopDirectoryOnly)
                       where c.Length > int.Parse(RetentionBakFileLength)
                       orderby DateTime.Parse(c.Replace(".rar", "").Replace("_", "-").Replace(bakrarPath, "")) descending
                       select c;

            Trace.WriteLine(list.Count());