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; }
public DateTime cdate { get; set; }
public DateTime mdate { get; set; }
public DateTime adate { get; set; }
public Int64 size { get; set; }
}
I have a screen on which users can select what they want. At the moment I go through a filter system:
foundfiles = new BindingList<f_results>(totalresults.Find(fname.Text,true));
if (fsize.Text.Trim() != "")
{
try
{
Int64 sz = Int64.Parse(fsize.Text);
List<f_results> y = (from p in foundfiles where p.size >= sz orderby p.size descending select p ).ToList();
foundfiles = new BindingList<f_results>(y);
}
catch
{ }
}
if (adate.Text.Trim() != "")
{
try
{
List<f_results> y;
DateTime test = DateTime.Parse(adate.Text);
if ((adateop.Text) == ">")
{
y = (from p in foundfiles where p.adate >= test select p).ToList();
}
else
y = (from p in foundfiles where p.adate <= test select p).ToList();
foundfiles = new BindingList<f_results>(y);
}
catch
{ }
}
if (mdate.Text.Trim() != "")
{
try
{
List<f_results> y;
DateTime test = DateTime.Parse(mdate.Text);
if ((mdateop.Text) == ">")
{
y = (from p in foundfiles where p.mdate >= test select p).ToList();
}
else
y = (from p in foundfiles where p.mdate <= test select p).ToList();
foundfiles = new BindingList<f_results>(y);
}
catch
{ }
}
if (cdate.Text.Trim() != "")
{
try
{
List<f_results> y;
DateTime test = DateTime.Parse(cdate.Text);
if ((cdateop.Text) == ">")
{
y = (from p in foundfiles where p.cdate >= test select p).ToList();
}
else
y = (from p in foundfiles where p.cdate <= test select p).ToList();
foundfiles = new BindingList<f_results>(y);
}
catch
{ }
}
At the end, I have my results the way I want them, but I’m looking to process about 72 TB of file data so, there are plenty of files and plenty of directories in my list (totalresults is a type results, which contains a list of files (f_results) and directories (results).. Find then iterates down and returns a massive list of f_results which match a given regex.
Is there a way to make my LINQ query one query? Given not all options maybe used, e.g. they may just want files > x, or not used since.. or.. etc.
I did consider making flags for the test, and so on, as it’s the test part that’s the most important.. is that the better way, or is there better? Or does it not matter much in the wash?
You could generate your filters beforehand, then apply them all at once – you would only have to iterate your initial enumeration once, something like this (shortened):
More importantly don’t call
ToList()until you have processed all filters, otherwise you keep iterating through your full result list over and over.