我正在尝试编写一个非常简单的方法来匹配userId&返回相应的imagepath字符串。我的代码是这样的:
public string GetImagePath(int _ID)
{
var imagepath=from s in context.Userinfoes
where s.UserID==_ID
select s.UserImage;
return imagepath.ToString();
}
问题是linq没有加载数据,而是加载像“system.data.object.objectquery1 [system.string]”这样的字符串!我之前多次写过类似的方法但是第一次遇到这个问题。
FYKI:
我正在使用3层架构和在执行此操作之前更新了我的edmx。我删除了我的参考资料再次添加它们来解决问题但是没有用。
请帮忙。
答案 0 :(得分:3)
您的问题在return imagepath.ToString()
,而imagepath不是数据。它是IQueriable。所以ToString()返回" system.data.object.objectquery1 [system.string]"。
在此代码之前,您需要迭代查询。所以试试这个:
public string GetImagePath(int _ID)
{
var imagepath=from s in context.Userinfoes
where s.UserID==_ID
select s.UserImage;
return imagepath.FirstOrDefault().ToString();
}
答案 1 :(得分:2)
你需要使用Single()/ First()从ienumerable列表中返回一个项目