2017-04-15 2 views
0

私はMVCアプリケーションでEFコードファーストバックエンドで作業しています。カテゴリーやブランド別にページを設定してフィルタリングしたい商品テーブルがあります。LINQによる製品テーブルのフィルタリング

data table with filter

私はこれを行うための最善の方法であり、私の道のsillynessを指すように自由に感じかわからないんだけど、今の私は上記のページについては、以下のViewModelを持っている:

public class InventoryReportViewModel 
{ 
    public SearchViewModel Search { get; set; } // 2 string props [Type and Term] 
    public IEnumerable<ProductViewModel> Products { get; set; } 
    public PaginationViewModel Pagination { get; set; } // 3 int props [currentPage, recordsPerPage, totalRecords] 
} 
私はここに私のLINQクエリを構築し、自分自身を繰り返したくない、とどの程度スマートになりたい

は私の試みです:

public InventoryReportViewModel GetProducts(int page, string searchTerm, string searchType) 
{ 
    var activeProducts = _context.Products.Where(p => !p.IsDeleted); 
    if (!string.IsNullOrEmpty(searchTerm)) 
    { 
    if (searchType == "category") 
    { 
     activeProducts.Where(
      p => string.Equals(p.Category.Name, searchTerm.Trim(), StringComparison.CurrentCultureIgnoreCase)) 
     .OrderBy(p => p.Category.Name) 
     .Skip(_recordsPerPage * (page - 1)) 
     .Take(_recordsPerPage); 
    } 
    else 
    { 
     activeProducts.Where(
      p => string.Equals(p.Brand.Name, searchTerm.Trim(), StringComparison.CurrentCultureIgnoreCase)) 
     .Skip(_recordsPerPage * (page - 1)) 
     .Take(_recordsPerPage); 
    } 
    } 
    else 
    { 
    activeProducts.Skip(_recordsPerPage * (page - 1)).Take(_recordsPerPage); 
    } 

    var productPageVm = new InventoryReportViewModel 
    { 
    Products = ProductViewModelFactory.BuildListOfProductViewModels(activeProducts), 
    Pagination = new PaginationViewModel 
    { 
     CurrentPage = page, 
     RecordsPerPage = _recordsPerPage, 
     TotalRecords = _context.Products.Count(p => p.Quantity > 0 && !p.IsDeleted) 
    } 
    }; 
    return productPageVm; 
} 

しかし、上記のようではありません。仕事に!それが私のrecordsPerPageであるので、私はわずか10を取得する必要があるとき、私は200の製品がViewModelで返されます。

どこが間違っていますか?

+0

? –

+0

web.configから私はコンストラクタでそれを読んでいます。私はデバッグして、すべての私のページネーションのプロパティは、彼らがする必要があることを確認することができます – Ciwan

答えて

1

LINQメソッドは、適用するシーケンスを変更しません。戻り値として新しいシーケンスを生成します。 LINQ操作の戻り値を使用する必要があります。 activeProductsは、メソッド呼び出しの影響を受けません。

例:あなたはこのrecordsPerPage変数に値を渡している

var activeProducts = _context.Products.Where(p => !p.IsDeleted); 
if (!string.IsNullOrEmpty(searchTerm)) 
{ 
    if (searchType == "category") 
    { 
     // See the change here? 
     activeProducts = activeProducts 
      .Where(p => string.Equals(
       p.Category.Name, 
       searchTerm.Trim(), 
       StringComparison.CurrentCultureIgnoreCase)) 
      .OrderBy(p => p.Category.Name) 
      .Skip(_recordsPerPage * (page - 1)) 
      .Take(_recordsPerPage); 
    } 
    else 
    { 
     // Here. 
     activeProducts = activeProducts 
     .Where(p => string.Equals(
      p.Brand.Name, 
      searchTerm.Trim(), 
      StringComparison.CurrentCultureIgnoreCase)) 
     .Skip(_recordsPerPage * (page - 1)) 
     .Take(_recordsPerPage); 
    } 
} 
else 
{ 
    // And here. 
    activeProducts = activeProducts 
     .Skip(_recordsPerPage * (page - 1)) 
     .Take(_recordsPerPage); 
} 
+0

私は新しい変数にLINQにやっている変更を割り当てる? – Ciwan

+0

必要に応じて、 'activeProducts'変数に再割り当てすることができます。例で更新されました。 –

+0

これはうまくいった、ありがとう – Ciwan

関連する問題