【问题标题】:How can I use conditional Order with EntityFramework/linq如何在实体框架/linq 中使用条件订单
【发布时间】:2020-08-06 14:50:12
【问题描述】:

每当我有很多订购和过滤选项时,在我的存储库中我都会使用“switch”来决定,但代码非常重复。
如何使用 coditional order 删除重复的代码? 你有解决方案来优化这段代码吗?

示例代码:

IEnumerable<Products> products;
switch (orderBy)
{
    case "Create":
        products = _GFazContext.Products
             //... many conditions like:            
             .Where(r => (productId == null || r.ProductId == productId))//Duplicate code
             .Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
             .Where(r => (statusId == null || r.StatusId == statusId))  //Duplicate code
                                                                        //Condition about the order 
             .Where(r => (data1 == null || r.CreateDate >= data1))
             .Where(r => (data2 == null || r.CreateDate <= data2))
             .Include(r => r.Group)    //Duplicate code

             //Specific order       
             .OrderBy(p => p.CreateDate)
             .Skip(pagination.SkipRecords)//Duplicate code
             .Take(pagination.PageSize);//Duplicate code
        break;

    case "Update":
        products = _GFazContext.Products
             //... many conditions like:            
             .Where(r => (productId == null || r.ProductId == productId))//Duplicate code
             .Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
             .Where(r => (statusId == null || r.StatusId == statusId))  //Duplicate code

             //conditions about the order   
             .Where(r => (data1 == null || r.LastUpdate >= data1))
             .Where(r => (data2 == null || r.LastUpdate <= data2))
             .Include(r => r.Group) //Duplicate code

             //Specific orders
             .OrderBy(p => p.LastUpdate)
             .Skip(pagination.SkipRecords)//Duplicate code
             .Take(pagination.PageSize);//Duplicate code
        break; break;
    default:
        products = _GFazContext.Products
             //... many conditions like:            
             .Where(r => (productId == null || r.ProductId == productId))//Duplicate code
             .Where(r => (groupId == null || r.GroupId == groupId))//Duplicate code
             .Where(r => (statusId == null || r.StatusId == statusId))  //Duplicate code
                                                                        //Condition about the order         
             .Where(r => (data1 == null || r.ReleaseDate >= data1))
             .Where(r => (data2 == null || r.ReleaseDate <= data2))
             .Include(r => r.Group) //Duplicate code

             //Specific orders
             .OrderBy(p => p.ReleaseDate)
             .Skip(pagination.SkipRecords)//Duplicate code
             .Take(pagination.PageSize);//Duplicate code
        break;
}
return products;

【问题讨论】:

    标签: c# linq optimization entity-framework-core


    【解决方案1】:

    类似的东西

    IQueryable<Products> products = _GFazContext.Products
        .Where(r => (productId == null || r.ProductId == productId))
        .Where(r => (groupId == null || r.GroupId == groupId))
        .Where(r => (statusId == null || r.StatusId == statusId));
    
    switch (orderBy)
    {
        case "Create":
            products = products
                    .Where(r => (data1 == null || r.CreateDate >= data1))
                    .Where(r => (data2 == null || r.CreateDate <= data2))
                    .Include(r => r.Group)
                    .OrderBy(p => p.CreateDate);
            break;
        case "Update":
            products = products
                    .Where(r => (data1 == null || r.LastUpdate >= data1))
                    .Where(r => (data2 == null || r.LastUpdate <= data2))
                    .Include(r => r.Group)
                    .OrderBy(p => p.LastUpdate);
            break;
        default:
            products = products
                    .Where(r => (data1 == null || r.ReleaseDate >= data1))
                    .Where(r => (data2 == null || r.ReleaseDate <= data2))
                    .Include(r => r.Group)
                    .OrderBy(p => p.ReleaseDate);
            break;
    }
    products = products
        .Skip(pagination.SkipRecords)
        .Take(pagination.PageSize);
    
    return products;
    

    【讨论】:

      【解决方案2】:

      试试这个 -

      IEnumerable<Products> products;
      
      products = _GFazContext.Products
                 .Where(r => (productId == null || r.ProductId == productId))
                 .Where(r => (groupId == null || r.GroupId == groupId))
                 .Where(r => (statusId == null || r.StatusId == statusId))  
                                                                                              
                 .Where(r => orderBy == "Create" ? (data1 == null || r.CreateDate >= data1) : (orderBy == "Update" ? (data1 == null || r.LastUpdate >= data1) : (data1 == null || r.ReleaseDate >= data1)))
                 .Where(r => orderBy == "Create" ? (data2 == null || r.CreateDate <= data2) : (orderBy == "Update" ? (data2 == null || r.LastUpdate <= data2) : (data2 == null || r.ReleaseDate <= data2)))
                 .Include(r => r.Group)    
      
                               //Specific order      
                 .OrderBy(p => orderBy == "Create" ? p.CreateDate : (orderBy == "Update"?  p.LastUpdate : p.ReleaseDate))
                 .Skip(pagination.SkipRecords)
                 .Take(pagination.PageSize);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-07
        • 2020-10-16
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多