Combine LINQ queries

后端 未结 3 854
情深已故
情深已故 2020-12-22 09:30

I\'m in the process of making a small query thing for a set of results of files.

public class f_results
    {
        public String name { get; set; }
               


        
3条回答
  •  时光取名叫无心
    2020-12-22 10:17

    At least I suggest to remove the .ToList() invokes everywhere. As LINQ has a deferred invocation, it will iterate once, even you have:

    var foundfiles = from p in foundfiles where p.size >= sz select p ;
    foundfiles = from p in foundfiles where p.mdate >= test select p
    

    Update (in that case order by should be put after all filters)

    But if you write:

    var foundfiles = (from p in foundfiles where p.size >= sz orderby p.size descending select p).ToList() ;
    foundfiles = (from p in foundfiles where p.mdate >= test select p).ToList();
    

    It will iterate two times - and that might be a serious performance problem.

    But I don't think the code will look much simpler if you make this code as a single query.

    Also, why are you catching all exceptions? You shouldn't do that.

提交回复
热议问题