首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用EF快捷查询生成?

如何用EF快捷查询生成?
EN

Stack Overflow用户
提问于 2014-04-30 11:17:28
回答 1查看 76关注 0票数 0

我有保存用户输入的模型,使用EF使用这些数据进行数据库查询构造。所有对角线都是可选的。

我的问题是:查询构建是否可以用更短的方式完成?

具有用户输入的模型:

代码语言:javascript
复制
public class PostFilterModel
{
    public PostFilterModel()
    {
        Currency = Enumerable.Empty<Currency>();
        Condition = Enumerable.Empty<Condition>();
        Transmission = Enumerable.Empty<Transmission>();
        Rudder = Enumerable.Empty<Rudder>();
        Body = Enumerable.Empty<Body>();
        Engine = Enumerable.Empty<Engine>();
        Gear = Enumerable.Empty<Gear>();
    }

public int City { get; set; }
public int Region { get; set; }
public int Brand { get; set; }
public int Model { get; set; }
public int MinHorsePower { get; set; }
public int MaxHorsePower { get; set; }
public int MinEngineCapacity { get; set; }
public int MaxEngineCapacity { get; set; }
public int MinMileage { get; set; }
public int MaxMileage { get; set; }
public int MinPrice { get; set; }
public int MaxPrice { get; set; }
public int MinYear { get; set; }
public int MaxYear { get; set; }

public IEnumerable<Currency> Currency { get; set; }
public IEnumerable<Condition> Condition { get; set; }
public IEnumerable<Transmission> Transmission { get; set; }
public IEnumerable<Rudder> Rudder { get; set; }
public IEnumerable<Body> Body { get; set; }
public IEnumerable<Engine> Engine { get; set; }
public IEnumerable<Gear> Gear { get; set; }
}

以及基于模型的查询生成器:

代码语言:javascript
复制
public ICollection<Post> GetByFilter(PostFilterModel filter)
        {
            IQueryable<Post> Posts = uow.PostRepository.GetAll();

            if (filter.City > 0)
                Posts = Posts.Where(p => p.City.CityId == filter.City);

            if (filter.Region > 0)
                Posts = Posts.Where(p => p.City.Region.RegionId == filter.Region);

            if (filter.Brand > 0)
                Posts = Posts.Where(p => p.Car.Brand.BrandId == filter.Brand);

            if (filter.Model > 0)
                Posts = Posts.Where(p => p.Car.Model.ModelId == filter.Model);

            if (filter.MinPrice > 0)
                Posts = Posts.Where(p => p.Price >= filter.MinPrice);

            if (filter.MaxPrice > 0)
                Posts = Posts.Where(p => p.Price <= filter.MaxPrice);

            if (filter.MinYear > 0)
                Posts = Posts.Where(p => p.Car.Year >= filter.MinYear);

            if (filter.MaxYear > 0)
                Posts = Posts.Where(p => p.Car.Year <= filter.MaxYear);

            if (filter.Condition.Count() > 0)
                Posts = Posts.Where(p => filter.Condition.Contains(p.Car.Condition));

            if (filter.Transmission.Count() > 0)
                Posts = Posts.Where(p => filter.Transmission.Contains(p.Car.Transmission));

            if (filter.Rudder.Count() > 0)
                Posts = Posts.Where(p => filter.Rudder.Contains(p.Car.Rudder));

            if (filter.Body.Count() > 0)
                Posts = Posts.Where(p => filter.Body.Contains(p.Car.Body));

            if (filter.Engine.Count() > 0)
                Posts = Posts.Where(p => filter.Engine.Contains(p.Car.Engine));

            if (filter.Gear.Count() > 0)
                Posts = Posts.Where(p => filter.Gear.Contains(p.Car.Gear));

            return Posts.ToList();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-01 05:49:49

我不知道您为什么要使用更少的代码,我更喜欢可读性而不是更少的代码。我唯一想做的事情是,在不破坏可读性的情况下,将所有逻辑合并到一个Where子句中,类似于:

代码语言:javascript
复制
    public ICollection<Post> GetByFilter(PostFilterModel filter)
    {
        return uow.PostRepository.GetAll().Where(p => 
            (filter.City <= 0 || p.City.CityId == filter.City) &&
            (filter.Region <= 0 || p.Region.RegionId == filter.Region) &&
            (filter.Brand <= 0 || p.Car.Brand.BrandId == filter.Brand) &&
            (filter.Model <= 0 || p.Car.Model.ModelId == filter.Model) &&
            (filter.MinPrice <= 0 || p.Price >= filter.MinPrice) &&
            (filter.MaxPrice <= 0 || p.Price <= filter.MaxPrice) &&
            (filter.MinYear <= 0 || p.Car.Year >= filter.MinYear) &&
            (filter.MaxYear <= 0 || p.Car.Year <= filter.MaxYear) &&
            (filter.Condition.Count() <= 0 || filter.Condition.Contains(p.Car.Condition)) &&
            (filter.Transmission.Count() <= 0 || filter.Transmission.Contains(p.Car.Transmission)) &&
            (filter.Rudder.Count() <= 0 || filter.Rudder.Contains(p.Car.Rudder)) &&
            (filter.Body.Count() <= 0 || filter.Body.Contains(p.Car.Body)) &&
            (filter.Engine.Count() <= 0 || filter.Engine.Contains(p.Car.Engine)) &&
            (filter.Gear.Count() <= 0 || filter.Gear.Contains(p.Car.Gear)) &&
             );
    }

但你并不是真的存钱。

您可以通过在筛选器上存储子句来使用查询的动态生成,但这只是将代码移到其他地方,并将依赖关系构建到代码中。

我恳请您不要使用单行if语句(即没有大括号),因为这是一个等待发生的错误。

我的观点是,保持代码的可读性,而不是简短。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23386686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档