结合LINQ查询 [英] Combine LinQ queries

查看:94
本文介绍了结合LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜同胞 - 智能生物。

Hi fellow intellegent beings.

林在做一个小的查询,事情一组文件的结果的过程。

Im 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; }
    }



所以,我有一个屏幕,用户可以选择他们想要的东西。目前,我经过一个过滤系统

So, I have a screen 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
        { }
    }



最后,我有我的结果我想要的方式,但是我希望处理有关文件的数据,这样的72TB,那里有大量的文件和大量的目录在我的名单(使用totalResults是一种结果,其中包含的文件列表(f_results)和目录(结果)。查找然后itterates下来,并​​返回匹配给定的正则表达式

At the end, I have my results the way I want them, but Im looking to process about 72tb of file data so, theres 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 itterates down and returns a massive list of f_results which match a given regex

反正是有使我的LINQ查询这f_results的一个巨大的名单, 1查询?鉴于并非所有选项也许使用,例如,他们可能只是想文件> x或不使用以来..或者..等等

Is there anyway to make my LinQ query, 1 query? Given not all options maybe used, eg they may just want files > x, or not used since.. or.. etc

我没有考虑标志测试等,其测试部分这就是最重要的..是更好的方法,或者是有更好?或者它没有多大关系在洗?

I did consider making flags for the test, and so on, as its the test part thats 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):

IEnumerable<f_results> foundfiles = new List<f_results>();
var filters = new List<Func<f_results, bool>>();

if (fsize.Text.Trim() != "")
{
    long sz = long.Parse(fsize.Text);
    filters.Add(x => x.size >= sz);
}

if (adate.Text.Trim() != "")
{
    DateTime test = DateTime.Parse(adate.Text);
    filters.Add(x => x.adate >= test);
}

foreach (var filter in filters)
{
    var filterToApply = filter;
    foundfiles = foundfiles.Where(filterToApply);
}
finalResults = new BindingList<f_results>(foundfiles);



更​​重要的是不要叫了ToList(),直到处理完所有过滤器,否则您保留通过完整的结果列表迭代一遍又一遍。

More importantly don't call ToList() until you have processed all filters, otherwise you keep iterating through your full result list over and over.

这篇关于结合LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆