C# linq c中的动态多where子句#

C# linq c中的动态多where子句#,c#,linq,reflection,where-clause,expression-trees,C#,Linq,Reflection,Where Clause,Expression Trees,我有一个使用linq的请求查询。查询有多个where子句,表示返回匹配名称和城市的项目列表。 下面是我用于multiplewhere子句的一段代码,但它返回一组空项。 其中字段包含字段名列表,如名称;城市 其中FieldValue包含类似james的字段值列表;德里 var where = FilterLinq<T>.GetWherePredicate(wherefield, wherefieldvalue).Compile(); items = items.Where(wher

我有一个使用linq的请求查询。查询有多个where子句,表示返回匹配名称和城市的项目列表。 下面是我用于multiplewhere子句的一段代码,但它返回一组空项。 其中字段包含字段名列表,如名称;城市 其中FieldValue包含类似james的字段值列表;德里

 var where = FilterLinq<T>.GetWherePredicate(wherefield, wherefieldvalue).Compile();
 items = items.Where(where).OrderByDescending(a => a.GetType().GetProperty(field).GetValue(a, null)).Skip

 public class FilterLinq<T>
 {
    public static Expression<Func<T, Boolean>> GetWherePredicate(string whereFieldList, string whereFieldValues)
    {
        //the 'IN' parameter for expression ie T=> condition
        ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);

        //combine them with and 1=1 Like no expression
        Expression combined = null;
        if (whereFieldList != null)
        {
            string[] field = whereFieldList.Split(';');
            string[] fieldValue = whereFieldValues.Split(';');
            for (int i = 0; i < field.Count(); i++)
            {
                //Expression for accessing Fields name property
                Expression columnNameProperty = Expression.Property(pe, field[i]);

                //the name constant to match 
                Expression columnValue = Expression.Constant(fieldValue[i]);

                //the first expression: PatientantLastName = ?
                Expression e1 = Expression.Equal(columnNameProperty, columnValue);

                if (combined == null)
                {
                    combined = e1;
                }
                else
                {
                    combined = Expression.And(combined, e1);
                }
            }
        }

        //create and return the predicate
        return Expression.Lambda<Func<T, Boolean>>(combined, new ParameterExpression[] { pe });
    }
}
var where=FilterLinq.GetWherePredicate(wherefield,wherefieldvalue).Compile();
items=items.Where(Where).OrderByDescending(a=>a.GetType().GetProperty(field).GetValue(a,null)).Skip
公共类过滤器LINQ
{
公共静态表达式GetWherePredicate(字符串whereFieldList、字符串whereFieldValues)
{
//表达式ie T=>条件的“IN”参数
ParameterExpression pe=表达式.参数(typeof(T),typeof(T).Name);
//将它们与and 1=1组合,就像没有表达式一样
表达式组合=空;
if(whereFieldList!=null)
{
string[]field=whereFieldList.Split(“;”);
string[]fieldValue=whereFieldValues.Split(“;”);
for(int i=0;i
您的示例有效。当然,它只适用于字符串属性,但我假设这是您的用例。 也许它没有达到您想要的效果,因为您将子句与and结合在一起,但实际上您确实希望使用Or,JU更改此代码行:

combined = Expression.And(combined, e1);


代码比我的口味需要多一点,但只要字段是
string
type`(
Expression.And
应该是
Expression.AndAlso
,但这不会改变结果),就不会有明显的错误。你能发布一个复制该问题的示例吗?代码的第二行似乎不完整。这可能是一个愚蠢的问题,但您确定where子句过滤掉了所有元素吗?问题是否可能与行末的
Skip
语句有关?谢谢大家提供的详细信息。我有一个whereField是int,另一个是string。我将尝试将int转换为string,然后进行检查
combined = Expression.Or(combined, e1);