我应该将这种类型显式转换为什么?
To what should I explicitly convert this type?
使用此代码:
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"
}
}
我需要做什么才能让我的 Controller 方法传回查询的四个值?
您正在选择 GenerateVals
的合集。您需要将变量修改为 IEnumerable<GenerateVals>
或将查询修改为仅 return 一项(例如,使用 .Single()
或 .First()
)。
将 UnitReportPairGenerateValsModel.GenerateVals
更改为 IEnumerable<UnitReportPairGenerateValsModel.GenerateVals>
。 select returns 为投影指定类型的 IEnumerable。
IEnumerable<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()
});
如果你很懒,把所有东西都做成数组:
public class UnitReportPairGenerateValsModel
{
public GenerateVals[] generatevals { get; set; }
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()
}).ToArray();
使用此代码:
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"
}
}
我需要做什么才能让我的 Controller 方法传回查询的四个值?
您正在选择 GenerateVals
的合集。您需要将变量修改为 IEnumerable<GenerateVals>
或将查询修改为仅 return 一项(例如,使用 .Single()
或 .First()
)。
将 UnitReportPairGenerateValsModel.GenerateVals
更改为 IEnumerable<UnitReportPairGenerateValsModel.GenerateVals>
。 select returns 为投影指定类型的 IEnumerable。
IEnumerable<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()
});
如果你很懒,把所有东西都做成数组:
public class UnitReportPairGenerateValsModel
{
public GenerateVals[] generatevals { get; set; }
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()
}).ToArray();