2011-08-03 39 views
0

我想在MVC網站中使用MongoDB和C#驅動程序顯示搜索排名以及結果。 我的目標是顯示網格是這樣的:使用MongoDB和C#驅動程序計算排名

  1. 這是導致一個
  2. 這是導致2
  3. 這是導致3

我的模型:

public class Product 
{ 
    [BsonId] 
    public string Id { get; set; } 

    public string Name { get; set; } 

    public int Rank { get; set; } 
} 

我從存儲庫層查找代碼如下所示:

public IList<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class 
    { 
     return this.GetQuery<TEntity>().AsQueryable().Where(criteria).ToList<TEntity>(); 
    } 

我的控制器看起來像這樣:

public ActionResult Index(string query) 
    { 
     var model = new SearchModel(); 

     model.Results = this.Repository.Find<Product>(x => x.Name == 「some query」) 
      .OrderBy(model.GridSortOptions.Column, model.GridSortOptions.Direction) 
      .AsPagination(1, 25); 

     return View(model); 
    } 

的Mongo.Find命令需要來用每個記錄的模型和計算秩(1,2,3等)。

如何使用C#驅動程序來解決這個問題?我也使用流利的linq提供程序。

回答

1

mongodb中沒有Rank函數,因此驅動程序也不支持它。但我想這不是一個問題,因爲當你加載數據或者你將顯示一個網格時,你可以在客戶端建立行級別。

var pagingSkip = 1; 
model.Results = this.Repository.Find<Product>(x => x.Name == 「some query」) 
    .OrderBy(model.GridSortOptions.Column, model.GridSortOptions.Direction) 
    .AsPagination(pagingStart, 25); 

foreach(var item in model.Results) 
{ 
    item.Rank = pagingSkip + 1; 
} 
+0

我正在考慮這樣做。雖然有點醜陋。謝謝! – rboarman

+0

@rboarman:不不,不醜。你可以使用sql作爲替代,Rank函數在那裏;)。不用謝。 –