1

我有以下带有 linq 查询的代码:

class Program
{
    static void Main(string[] args)
    {
        var allProfessionals = new Collection<Professional>
         {
             new Professional { Name = "Bruno Paulovich Silva" },
             new Professional { Name = "Ivan Silva Paulovich Bruno"},
             new Professional { Name = "Camila Campos"}
         };

        var namesSearch = new[] {"bruno", "silva"};

        var query = namesSearch.Aggregate(allProfessionals.AsQueryable(), (current, nome) => current.Where(oh => oh.Name.ToLower().Contains(nome.ToLower())));

        foreach (var res in query.ToList())
        {
            Console.WriteLine(res.Name.ToLower());
        }
    }
}

结果是:

Bruno Paulovich Silva
Silva Paulovich Bruno

我想知道如何将 linq 查询聚合转换为可以在其他时间重用的通用方法。

在下面的示例中,我展示了我对项目中使用的通用查询的理解:

public IQueryable<T> QueryBy(Expression<Func<T, bool>> criteria)
{
    return DbSet.Where(criteria);
}

ps:对不起我的英语不好

4

2 回答 2

2

坚持基本要素和与脚本相同的输出,它将类似于:

class Program
{
    static void Main(string[] args)
    {
        var allProfessionals =
            new Collection<Professional>
                {
                    new Professional {Name = "Bruno Paulovich Silva"},
                    new Professional {Name = "Ivan Silva Paulovich Bruno"},
                    new Professional {Name = "Camila Campos"}
                };

        var namesSearch = new[] {"bruno", "silva"};

        var items = allProfessionals
            .Select(x => x.Name)
            .ContainsAll(namesSearch);

        foreach (var res in items)
        {
            Console.WriteLine(res);
        }
    }
}

static class Extensions
{
    public static IEnumerable<string> ContainsAll(this IEnumerable<string> haystacks, IEnumerable<string> needles)
    {
        var lowerNeedles = needles.Select(x => x.ToLower()).ToList();
        var lowerHay = haystacks.Select(x => x.ToLower()).ToList();

        // note that Regex may be faster than .Contains with larger haystacks
        return lowerNeedles
            .Where(hay => lowerHay.All(hay.Contains)); // or .Any(), depending on your requirements
    }
}

请注意,如果您使用 LinqToSQL 或类似技术,则可能不会使用表索引。这可能会使查询变得非常慢。

为了使其符合所提到的QueryBy<T>,它可能看起来像:

        var items = allProfessionals.QueryBy(
            professional => namesSearch
                .Select(needle => needle.ToLower()) // convert all to lower case
                .All(hay => professional.Name.ToLower().Contains(hay))); // then try to search for a professional that matches all nameSearch.
于 2013-03-08T21:53:25.167 回答
0

这是我如何实现 LikeBy 的方法,其中字符串数组等效于包含所有内容。

public IQueryable<T> LikeBy(string[] strings, Func<IQueryable<T>, string, IQueryable<T>> criteria)
{
    return strings.Aggregate(GetAll(), criteria);
}

这里是如何实现的例子:

public IQueryable<ViaturaFuncao> FindByNome(string names)
{
    return Uow.Professional.LikeBy(names.Trim().Split(' '),
                                       (professional, nameProfessional) =>
                                       professional.Where(
                                           f => f.Name.ToLower().Contains(nameProfessional.ToLower())));
}

PS:在项目中,我们使用 UnitOfWork,这就是为什么在实体之前有 UOW

于 2013-03-12T20:22:20.957 回答