簡體   English   中英

C#,Linq,動態查詢:用於過濾存儲庫外部的動態查詢的代碼

[英]C#, Linq, Dynamic Query: Code to filter a Dynamic query outside of the Repository

如果您在存儲庫中執行以下操作:

IQueryable<CarClass> GetCars(string condition, params object[] values) {
    return db.Cars.Where(condition, values);
}

然后在存儲庫外部設置條件和值:

string condition = "CarMake == @Make";
object[] values = new string[] { Make = "Ford" };

var result = myRepo.GetCars( condition, values);

如何使用Dynamic Query在存儲庫之外對結果進行排序?

return View( "myView", result.OrderBy("Price"));

當數據從存儲庫退出時,我不知何故失去了DynamicQuery性質。 是的,我還沒有找到如何返回通常要執行Select New Carclass的CarClass類型的方法。{fieldName = m.fieldName,...}

動態查詢要求:

  • 源是IQueryable<T> (因此,如果它是IEnumerable<T>或類似的,只需對其調用.AsQueryable()
  • 要執行動態查詢的代碼中引用的一個額外的dll
  • 適當的using指令放在本地源文件的頂部

選中這三個,您應該可以添加.Where(condition) .OrderBy(name)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM