2016-04-25 55 views
-2

有了這個代碼:我應該明確地轉換這種類型?

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" 
    } 
} 

什麼我需要做的,讓我的控制器方法,傳回查詢四個值?

+0

告訴我們你的'DbContext'和DbSet –

回答

2

您正在選擇GenerateVals的集合。您需要將變量修改爲IEnumerable<GenerateVals>或修改查詢以僅返回一個項目(例如,使用.Single().First())。

+1

因爲它應該不會超過一組被返回的丘壑,我喜歡「單身「 - 對我來說,這是一場全壘打。 –

2

更改UnitReportPairGenerateValsModel.GenerateValsIEnumerable<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() 
        }); 
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(); 
相關問題