0

我有一个项目列表,称为翻译。我根据搜索框中的搜索词过滤翻译,目前它看起来像这样:

private static IList<Translation> FilterTranslationListOLD(string filter, IEnumerable<Translation> translationList)
{
    filter = filter.ToLower();
    if (!string.IsNullOrEmpty(filter))
    {

        return translationList
            .Where(t => (t.Tag.Filename.ToLower().Contains(filter)
                            || t.Tag.FilePath.ToLower().Contains(filter)
                            || t.Tag.TagContent.ToLower().Contains(filter)
                            || (t.Tag.SanitizedTagContent != null && t.Tag.SanitizedTagContent.ToLower().Contains(filter))
                            || (t.TagTranslation != null && t.TagTranslation.ToLower().Contains(filter))
                            || (t.SanitizedTranslation != null && t.SanitizedTranslation.ToLower().Contains(filter))))
            .OrderBy(t => t.Tag.FilePath)
            .ThenBy(t => t.Tag.Filename).ThenBy(t => t.Tag.Id).ToList();
    }
    return translationList.OrderByDescending(t => t.DateTranslated).ToList();
}

我现在介绍了使用多个关键字进行搜索的功能,如下所示:

private static IList<Translation> FilterTranslationList(string filter, IEnumerable<Translation> translationList)
        {
            filter = filter.ToLower();
            var splitFilterTerms = filter.Split(',');

            if (splitFilterTerms.Any(split=>!string.IsNullOrEmpty(split)))
            {
                var translationListResults = new List<Translation>();
                foreach (var splitFilterTerm in splitFilterTerms)
                {
                   translationListResults.AddRange(translationList
                   .Where(t => (t.Tag.Filename.ToLower().Contains(splitFilterTerm)
                                   || t.Tag.FilePath.ToLower().Contains(splitFilterTerm)
                                   || t.Tag.TagContent.ToLower().Contains(splitFilterTerm)
                                   || (t.Tag.SanitizedTagContent != null && t.Tag.SanitizedTagContent.ToLower().Contains(splitFilterTerm))
                                   || (t.TagTranslation != null && t.TagTranslation.ToLower().Contains(splitFilterTerm))
                                   || (t.SanitizedTranslation != null && t.SanitizedTranslation.ToLower().Contains(splitFilterTerm))))
                   .OrderBy(t => t.Tag.FilePath)
                   .ThenBy(t => t.Tag.Filename).ThenBy(t => t.Tag.Id).ToList());
                }
                return translationListResults;
            }
            return translationList.OrderByDescending(t => t.DateTranslated).ToList();
        }

我想知道的是,有没有更好的写法?虽然它有效,但很高兴知道如何在所有 linq 中执行此操作或稍微减少/重构它(让它更整洁一些)。提前致谢!

4

2 回答 2

1

尝试查看SelectMany,这将帮助您展平一系列序列:

private static IList<Translation> FilterTranslationListOLD(string filter, IEnumerable<Translation> translationList)
{
    filter = filter.ToLower();
    var splitFilterTerms = filter.Split(',');
    if (splitFilterTerms.Any(split=>!string.IsNullOrEmpty(split)))
     {
        return splitFilterTerms.SelectMany(f => translationList
            .Where(t => (t.Tag.Filename.ToLower().Contains(f)
                            || t.Tag.FilePath.ToLower().Contains(f)
                            || t.Tag.TagContent.ToLower().Contains(f)
                            || (t.Tag.SanitizedTagContent != null && t.Tag.SanitizedTagContent.ToLower().Contains(f))
                            || (t.TagTranslation != null && t.TagTranslation.ToLower().Contains(f))
                            || (t.SanitizedTranslation != null && t.SanitizedTranslation.ToLower().Contains(f))))
            .OrderBy(t => t.Tag.FilePath)
            .ThenBy(t => t.Tag.Filename).ThenBy(t => t.Tag.Id)).ToList();
    }
    return translationList.OrderByDescending(t => t.DateTranslated).ToList();
}

我实际上并没有运行此代码。 这是 SelectMany 的 MSDN 文档

于 2013-05-09T14:21:58.533 回答
0

上面代码的问题是它显示在 OR 条件而不是 AND... 所以如果你有这些项目:

List<Car> Cars = new List<Car>();
Cars.Add(new Car(){Manufacturer="Mercedes Benz", Color="Blue"});
Cars.Add(new Car(){Manufacturer="Mercedes Benz", Color="Green"});

如果过滤器是“Mercedes Blue”,它将同时显示

我使用此代码仅显示蓝色汽车:

string Filter = "Mercedes blue";
List<string> SplittedFilter = new List<string>(Filter.ToLowerInvariant().Split(' '));

List<Car> FilteredCars = Cars;

foreach (var item in SplittedFilter) {
    FilteredCars = (from c in FilteredCars
                   where c.Manufacturer.ToLowerInvariant().Contains(item) || c.Color.ToLowerInvariant().Contains(item)
                   select c).ToList();

}

来源:http ://www.groggyjava.tv/blog/?p=84

于 2013-11-15T10:50:21.877 回答