我在模型中定义了一个类:
public class OfficeData
{
public int ReportID;
public DateTime? LastReported;
public string ReportedBy;
}
和此方法在模型中实例化类:
public IEnumerable<OfficeData> GetOfficeData(int OfficeId)
{
MyContext db = new MyContext();
var officeData = db.Database.SqlQuery<OfficeData>("get_data @OfficeID", new SqlParameter("OfficeID", OfficeId)).Select(data => new OfficeData { ReportID = data.ReportID, LastReported = data.LastReported, ReportedBy = data.ReportedBy};
return officeData;
}
我在控制器中调用该方法:
public ActionResult Create()
{
OfficeModel officeModel = new OfficeModel;
...
MyServiceLayer serv = new MyServiceLayer; // the service layer class holds the method
var officeData = serv.GetOfficeData(officeModel.OfficeID);
officeModel.ReportID = officeData.ReportID;
officeModel.LastReported = officeData.LastReported;
officeModel.ReportedBy = officeData.ReportedBy;
...
}
这是我在MVC的第一个项目,我想了解这里发生了什么。提前感谢您的帮助。
答案 0 :(得分:0)
GetOfficeData
会返回IEnumerable<OfficeData>
:
public IEnumerable<OfficeData> GetOfficeData(int OfficeId)
所以officeData
的推断类型也是IEnumerable<OfficeData>
。如果您将鼠标悬停在var
关键字上(或officeData
,我会忘记您必须将鼠标悬停在其上),就可以看到这一点。
var officeData = serv.GetOfficeData(officeModel.OfficeID);
因此编译器抱怨IEnumerable<OfficeData>.ReportID
不存在。这很可能不会。你的意思是通过调用FirstOrDefault()
或循环来调用Select
或者可能通过集合进行枚举吗?