我有一個Get
方法有工作分頁,但我也想創建一個方法,可以返回多個相同的基本類型的實體,仍然有分頁。我可以在IQueryable中使用multilpe EF實體來實現分頁嗎?
我知道這必須轉化爲SQL不知何故,我想將不得不作爲多個結果集返回。
// Model classes
public class Food
{
string Name {get;set;}
}
public class Fruit : Food
{
bool IsSweet {get;set;}
}
public class Vegetable : Food
{
bool IsGross {get;set;}
}
// FruitRepo -- BLL layer
public List<Fruit> GetFruit(int userId, int page, int itemsPerPage)
{
var query = repo.Get<Fruit>(e => e.userId == userId);
var dbResults = query.Skip((itemsPerPage * page) - itemsPerPage).Take(itemsPerPage).ToList();
return dbResults;
}
// FoodRepo -- BLL layer
public List<Food> GetFood(int userId, int page, int itemsPerPage)
{
// How can I use IQuerable here to implement paging correctly?
List<Food> rv = new List<Food>();
var repo = new Repo();
rv.AddRange(repo.Get<Fruit>(e => e.userId == userId).ToList());
rv.AddRange(repo.Get<Vegetable>(e => e.userId == userId).ToList());
return rv;
}
我相信這就是我要找的。我一直以Table per Entity的方式完成,但這看起來很有希望。我看到的唯一可能的缺點是將許多表/實體組合在一起可以構成一個非常寬泛的表,任何其他事物或限制要意識到代碼或數據庫端? – user3953989
Table-Per-Type與Table-Per-Hierarchy不應該對分頁結果有任何影響。您應該能夠使用繼承模式進行分頁。 –
@BradleyUffner我的意思是一般的任何效果,而不是特定於分頁。 – user3953989