Как я могу использовать условный заказ с EntityFramework / linq - PullRequest
0 голосов
/ 05 августа 2020

Когда-либо, когда у меня было много вариантов для сортировки и фильтрации, в моем репозитории я использовал "переключатель", чтобы решить, но код очень дублирован. Как я могу использовать кодовое распоряжение для удаления повторяющегося кода? У вас есть решение для оптимизации этого кода?

Пример кода:

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;

Ответы [ 2 ]

1 голос
/ 05 августа 2020

Примерно так

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;
0 голосов
/ 05 августа 2020

Попробуйте это -

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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...