有了這個代碼:我應該明確地轉換這種類型?
UnitReportPairGenerateValsModel.GenerateVals generateVals =
(from DataRow row in UnitReportPairGenerateValsDT.Rows
select new UnitReportPairGenerateValsModel.GenerateVals
{
DayOfMonth = Convert.ToInt32(row["DayOfMonth"]),
PatternOrdinal = row["PatternOrdinal"].ToString(),
PatternDOW = row["PatternDOW"].ToString(),
PatternInterval = row["PatternInterval"].ToString()
});
...我得到這個編譯器錯誤:
無法隱式轉換類型 'System.Collections.Generic.IEnumerable' 到 'WebAppRptScheduler.Models.UnitReportPairGenerateValsModel.GenerateVals' 。存在明確的轉換(您是否缺少演員?)
我需要什麼才能將其投入使用?
更多的上下文,這裏是整個控制器:
public class UnitReportPairGenerateValsController : Controller
{
public JsonResult GetUnitReportPairGenerateVals(string unit, string report)
{
UnitReportPairGenerateValsModel model = new UnitReportPairGenerateValsModel();
try
{
int rptId = SQL.GetReportIDForName(report);
string qry = string.Format(SQL.UnitReportPairGenerateQuery, unit, rptId);
DataTable UnitReportPairGenerateValsDT = SQL.ExecuteSQLReturnDataTable(
qry,
CommandType.Text,
null
);
UnitReportPairGenerateValsModel.GenerateVals generateVals =
(from DataRow row in UnitReportPairGenerateValsDT.Rows
select new UnitReportPairGenerateValsModel.GenerateVals
{
DayOfMonth = Convert.ToInt32(row["DayOfMonth"]),
PatternOrdinal = row["PatternOrdinal"].ToString(),
PatternDOW = row["PatternDOW"].ToString(),
PatternInterval = row["PatternInterval"].ToString()
});
model.generatevals = generateVals;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Json(model, JsonRequestBehavior.AllowGet);
}
}
...和型號:
public class UnitReportPairGenerateValsModel
{
public GenerateVals generatevals { get; set; }
public class GenerateVals
{
public int DayOfMonth { get; set; }
public string PatternOrdinal { get; set; } // such as "First", "Third", "Last" etc.
public string PatternDOW { get; set; } // such as "Monday", "Tuesday" etc.
public string PatternInterval { get; set; } // either "Week" or "Month"
}
}
什麼我需要做的,讓我的控制器方法,傳回查詢四個值?
告訴我們你的'DbContext'和DbSet –