我有一个简单方法的测试控制器:
private ApplicationEntities _db = new ApplicationEntities();
public String GetSomeString()
{
return _db.someStoredProcedure(8).SingleOrDefault();
}
这在控制器中工作正常,并说该方法返回" Hello" 它出现在ViewModel中时会出现问题。 我把它简化为:
public class TestView
{
private ApplicationEntities _db = new ApplicationEntities();
private string smth;
public String Something
{
get {
//This line does not work here.
Error 5 'System.Data.Entity.Core.Objects.ObjectResult<string>' does not contain a definition for 'SingleOrDefault' and no extension method 'SingleOrDefault' accepting a first argument of type 'System.Data.Entity.Core.Objects.ObjectResult<string>' could be found (are you missing a using directive or an assembly reference?)
var d = _db.someStoredProcedure(8).SingleOrDefault();
//This works, since the result is always only 1 row
var a = _db.someStoredProcedure(8).GetEnumerator();
var b = a.MoveNext();
var c = a.Current;
return a.Current;
}
set { smth= value; }
}
public async static void getString()
{
var a = new TestView().Something;
}
}
问题是为什么SingleOrDefault()不能在这里工作? (没有可以轻松添加的程序集引用)以及如何更好地解决此ViewModel和getString方法?