I'm trying to call a business model class static method from my controller action method but Im failling. Please I need really your helps. Thanks
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;
}
}
This is because your static method can not be converted to SQL and executed by entity framework.
Pull your members down first as a list and then do the select since that will be executed by the server rather than at the SQL level.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.