0

像这样的东西:

public static class ExtensionMethods
{
    public static IQueryable<MovieList> FilterEntityCollection(this IQueryable<MovieList> movies, IEnumerable<Foo.Models.FilterCriteria> filters)
    {
        foreach (var filter in filters)
        {
            // Apply the filter to the collection.
            movies= movies.Where(x => x.filter.FieldName == filter.expr);
                                    //x."MovieTitle"       == "Jaws" 
        }

        return movies;
    }
}

这样的事情可以在 C# 中完成吗?

4

1 回答 1

0

使用LinqKit

有点像:

var predicate = PredicateBuilder.True<MovieList>();

foreach (var filter in filters)
{
    string filter1 = filter;
    predicate = predicate.And(a => a.filter.FieldName == filter1);
}
return movies.AsExpandable().Where(predicate);

LinqKit 谓词构建器

using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace LinqKit
{
    public static class PredicateBuilder
    {
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2);
        public static Expression<Func<T, bool>> False<T>();
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2);
        public static Expression<Func<T, bool>> True<T>();
    }
}
于 2013-04-11T20:51:25.837 回答