我应该明确地转换这种类型?

时间:2016-04-25 17:34:21

标签: c# asp.net-mvc implicit-conversion explicit-conversion

使用此代码:

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' to' WebAppRptScheduler.Models.UnitReportPairGenerateValsModel.GenerateVals'。存在显式转换(您是否错过了演员?)

我需要做什么才能让它发挥作用?

有关更多上下文,这里是整个Controller:

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方法传回所查询的四个值?

3 个答案:

答案 0 :(得分:3)

如果你懒惰,把所有东西都变成一个阵列:

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();

答案 1 :(得分:2)

您正在选择GenerateVals的集合。您需要将变量修改为IEnumerable<GenerateVals>或修改查询以仅返回一个项目(例如,使用.Single().First())。

答案 2 :(得分:2)

UnitReportPairGenerateValsModel.GenerateVals更改为IEnumerable<UnitReportPairGenerateValsModel.GenerateVals>。 select返回为投影指定的类型的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()
                    });