我有一個簡單Person View Model
如何將一個簡單的viewmodel轉換爲Func <T,bool>謂詞?
public class PersonViewModel
{
public int ID {get;set;}
public String FirstName {get;set;}
public String LastName {get;set;}
}
視圖有三個字段
我有我的PersonService
這個功能,使用了EF6的datacontext這被注入。
public IQueryable<Person> Search(Expression<Func<Person,bool>> predicate)
{
return dataContext.GetSet<Person>().Where(predicate);
}
現在我的MVC控制器實際上有PersonViewModel class
[HttpPost]
public ActionResult Search(PeopleSearchViewModel model)
{
if (ModelState.IsValid)
{
var listOfPersons= PersonService.Search(
//map viewmodel to model and search here)
//return the list and render a view etc...not important
}
}
工作那麼我想知道是它是否是一個好主意,如果我能以某種方式把PersonViewModel,創建一個Func<Person,bool>
謂詞,並將其傳遞到PersonService
進行搜索,而不是使用automapper將我的視圖模型映射到域模型?
感謝
它不會正常工作,因爲你發送'Func <>'到'Where'方法,然後所有的過濾將在.Net端完成,整個表將從DB加載,這是不好的。它應該是'Expression>' –
2014-10-26 21:47:30
對不起,我錯過了,我已經更新了代碼 - 儘管我不確定這是否是你的意思。 – 2014-10-26 21:52:10
是的,我的意思是。嗯,'搜索'方法用於過濾。它不需要'Person'類的實例,那麼你的問題是什麼?你想使用'PersonSearchViewModel'進行'搜索'過濾? – 2014-10-26 21:54:23