字符串linq到实体错误-json
我试图用一个信息窗口在谷歌地图上绘制各种标记。在我尝试通过控制器传递字符串之前,一切都正常 我得到以下错误: LINQ to Entities无法识别方法'System.String GetMainPhoto(Int32)'方法,此方法无法转换为存储表达式 我读过这篇文章,主要是因为ToString方法或其他一些方法不能有效使用。但是,我不确定在这种情况下如何纠正这个错误 基本上,我有一个db-PropertyPhoto,它保存图片的文件名。GetMainPhoto基本上查找所有行并返回主pic文件名字符串linq到实体错误-json,json,asp.net-mvc-3,linq,entity-framework,Json,Asp.net Mvc 3,Linq,Entity Framework,我试图用一个信息窗口在谷歌地图上绘制各种标记。在我尝试通过控制器传递字符串之前,一切都正常 我得到以下错误: LINQ to Entities无法识别方法'System.String GetMainPhoto(Int32)'方法,此方法无法转换为存储表达式 我读过这篇文章,主要是因为ToString方法或其他一些方法不能有效使用。但是,我不确定在这种情况下如何纠正这个错误 基本上,我有一个db-PropertyPhoto,它保存图片的文件名。GetMainPhoto基本上查找所有行并返回主pic
public string GetMainPhoto(int id)
{
return db.PropertyPhotos.Single(p => p.PropertyId == id && p.MainPic == true).PhotoLocation;
}
控制员名单如下:
public ActionResult Map()
{
if (Request.IsAjaxRequest())
{
var properties = websiteRepository.FindAllProperties();
var jsonProperties = from property in properties
select new JsonProperty
{
PropertyId = property.PropertyId,
NoOfBedroom = property.NoOfBedrooms,
Price = property.Price,
Address1 = property.PropertyAddress.Address1,
MainPicSrc = websiteRepository.GetMainPhoto(property.PropertyId),
Latitude = property.PropertyAddress.Latitude,
Longitude = property.PropertyAddress.Longitude
};
return Json(jsonProperties.ToList(), JsonRequestBehavior.AllowGet);
}
else
{
return View();
}
}
在投影到匿名类型之前,首先尝试通过调用
.ToList()
急切地加载数据,否则EF不知道如何翻译网站存储库。GetMainPhoto
调用SQL:
var properties = websiteRepository.FindAllProperties().ToList();
不过要小心。通过这样做,您可能会遇到问题,因为对于初始结果集的每个元素,您将发送一个SQL查询来获取MainPicSrc
属性
更好的方法是直接由数据库执行,而不使用websiteRepository.GetMainPhoto
调用