我正在尝试从我的控制器操作方法中调用业务模型类静态方法,但我很费劲。 我真的需要你的帮助。感谢
public ActionResult Index()
{
var model =
_db.members
.Include(m =>m.homeTownCity)
.Include(m => m.currentCity)
.OrderBy(m => m.fname)
.ThenBy(m => m.lname)
.Select(m => new MembersListViewModel
{
homeTown = m.homeTownCity.Name,
currentCity = m.currentCity.Name,
//Calling a business class static method
Name= MemberManagement.getFullname(m.lname,m.fname),
spoken = m.spoken,
hasPosted = m.posts.Count()
});
return View(model);
}
public class MemberManagement
{
public static string getFullname( string fname,string lname) {
string fullname = fname + " " + lname;
return fullname;
}
}
答案 0 :(得分:2)
这是因为您的静态方法无法转换为SQL并由实体框架执行。
首先将您的成员作为列表拉下来,然后执行select,因为它将由服务器而不是SQL级别执行。
var members =
_db.members
.Include(m =>m.homeTownCity)
.Include(m => m.currentCity)
.OrderBy(m => m.fname)
.ThenBy(m => m.lname).ToList();
var model = members.Select(m => new MembersListViewModel
{
homeTown = m.homeTownCity.Name,
currentCity = m.currentCity.Name,
//Calling a business class static method
Name= MemberManagement.getFullname(m.lname,m.fname),
spoken = m.spoken,
hasPosted = m.posts.Count()
}).ToList();