Dynamic Linq Build Where子句许多参数

本文关键字:许多 参数 子句 Where Linq Build Dynamic | 更新日期: 2023-09-27 17:58:54

我将解释我的问题

因此,首先,我将谓词Linq动态地用于Build Where子句。我必须动态构建,因为我不知道会有多少参数。让我举一个例子。然而,对于A列可以是一个参数,对于B列可以是两个参数,如选择的值"Gas"或"Oil",但这是一个大问题,我无法正确组合这两列。因此,此代码可以工作,但返回0项。但我知道有。

public List<CarEntity> GetSearchByKCriteria(int cityId, List<string> fuelType, List<string> gearType, List<string> budget,
        List<string> caroser, List<string> enginePower)
    {
        Expression<Func<Car, bool>> query = null;
        Expression<Func<Car, bool>> combine = null;
        foreach (var bud in budget)
        {
            if (budget.Count >= 1)
            {
                if (bud == "1")
                {
                    if (budget.Count > 1)
                    {
                        query = car => car.Budget >= 20000 && car.Budget <= 34999;
                    }
                    else
                    {
                        query = car => car.Budget >= 20000 && car.Budget <= 34999;
                    }
                }
                else if (bud == "2")
                {
                    if (query != null)
                    {
                        combine = car => (car.Budget >= 35000 && car.Budget <= 49999);
                        query = query.Or(combine);
                    }
                    else
                    {
                        query = car => car.Budget >= 35000 && car.Budget <= 49999;
                    }
                }
            }
        }
        foreach (var caros in caroser)
        {
            if (caros != "-1" && !string.IsNullOrEmpty(caros))
            {
                if (query != null)
                {
                    if (query.Expand().ToString().ToLower().Contains("karoser"))
                    {
                        combine = car => (car.Karoser == caros);
                        query = query.And(combine);
                    }
                    else
                    {
                        combine = car => car.Karoser == caros;
                        query = query.And(combine);
                    }
                }
                else
                {
                    query = car => car.Karoser == caros;
                }
            }
        }
        foreach (var fuel in fuelType)
        {
            if (fuel != "-1" && !string.IsNullOrEmpty(fuel))
            {
                if (query != null)
                {
                    if (query.Expand().ToString().ToLower().Contains("yakituru"))
                    {
                        combine = car => (car.YakitTuru==fuel);
                        query = query.Or(combine);
                    }
                    else
                    {
                        combine = car => car.YakitTuru == fuel;
                        query = query.And(combine);
                    }
                }
                else
                {
                    query = car => car.YakitTuru == fuel;
                }
            }
        }
        foreach (var gear in gearType)
        {
            if (gear!="-1"&& !string.IsNullOrEmpty(gear))
            {
                if (query != null)
                {
                    if (query.Expand().ToString().ToLower().Contains("sanzimantipi"))
                    {
                        combine = car => (car.SanzimanTipi == gear);
                        query = query.Or(combine);
                    }
                    else
                    {
                        combine = car => car.SanzimanTipi == gear;
                        query = query.And(combine);
                    }
                }
                else
                {
                    query = car => car.SanzimanTipi == gear;
                } 
            }
        }
        foreach (var engine in enginePower)
        {
            if (enginePower.Count >= 1)
            {
                if (engine == "1")
                {
                    if (query != null)
                    {
                        if (query.Expand().ToString().ToLower().Contains("silindirhacmi"))
                        {
                            combine = car => (car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600);
                            query = query.Or(combine);
                        }
                        else
                        {
                            combine = car => (car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600);
                            query = query.And(combine);
                        }
                    }
                    else
                    {
                        query = car => car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600;
                    }
                }
                if (engine == "3")
                {
                    if (query != null)
                    {
                        if (query.Expand().ToString().ToLower().Contains("silindirhacmi"))
                        {
                            combine = car => (car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800);
                            query = query.Or(combine);
                        }
                        else
                        {
                            combine = car => (car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800);
                            query = query.And(combine);
                        }
                    }
                    else
                    {
                        query = car => car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800;
                    }
                }
        }
        using (var context = DataContextFactory.CreateContext())
        {
            var result = (from fkCar in context.Car.Where(query)
                          join pkCarBrand in context.CarBrand on fkCar.Marka equals pkCarBrand.ID
                          where fkCar.IsActive == true
                          select new
                          {
                              entity = fkCar,
                              joinEntity = pkCarBrand
                          });
            List<CarEntity> theCarList = new List<CarEntity>();
            foreach (var item in result)
            {
                CarEntity theEntity = Mapper.Map(item.entity);
                theEntity.CarBrand = Mapper.Map(item.joinEntity);
                theCarList.Add(theEntity);
            }
            return theCarList;
        }
    }

因此,感谢您的回复,

Dynamic Linq Build Where子句许多参数

不久前我遇到了一个类似的挑战,我想为一个属性列出一个允许的值列表,如果匹配,关联的实例将通过过滤器。我想出了以下扩展方法:

static public Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
    if (null == valueSelector)
    {
        throw new ArgumentNullException("valueSelector");
    }
    if (null == values) { throw new ArgumentNullException("values"); }
    ParameterExpression p = valueSelector.Parameters.Single();
    if (!values.Any())
    {
        return e => false;
    }
    var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
    var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
    return Expression.Lambda<Func<TElement, bool>>(body, p);
}

这是基于在上发布的讨论和代码http://www.velocityreviews.com/forums/t645784-linq-where-clause.html