2014-03-13 89 views
1

請幫忙。我在ASP.NET MVC新,我試圖發送一個查詢到存儲庫,但它給了我一個錯誤:發送查詢模型庫

Errr 3 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

我使用的是一個架構類,只有我需要的列。這是我用於存儲庫的代碼。

public class SSGridRepository : SSGridIRepository 
{ 
    private DataClassesSSDataContext db; 

    public SSGridRepository() 
    { 
     db = new DataClassesSSDataContext(); 
    } 

    public IList<SSQuerySchema> ListAll() 
    { 

     var SSQuery = (from HISTORies in db.HISTORies 
         join SSes in db.SSes on HISTORies.WO equals SSes.WO 
         join SSCUSTOMs in db.SSCUSTOMs on SSes.WO equals SSCUSTOMs.WO 
         join StatusTables in db.StatusTables on new { STATUS = SSes.STATUS } equals new { STATUS = StatusTables.Status } 
         join StatusTable_1 in db.StatusTables on new { OLDSTATUS = HISTORies.OLDSTATUS } equals new { OLDSTATUS = StatusTable_1.Status } 
         join StatusTable_2 in db.StatusTables on new { NEWSTATUS = HISTORies.NEWSTATUS } equals new { NEWSTATUS = StatusTable_2.Status } 
         where 
         HISTORies.OLDSTATUS == "m" && 
         HISTORies.NEWSTATUS == "n" && 
         HISTORies.ACTION == "Change Job Status" && 
         HISTORies.OLDSTATUS != HISTORies.NEWSTATUS && 
         HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year || 
         HISTORies.OLDSTATUS != HISTORies.NEWSTATUS && 
         HISTORies.NEWSTATUS == "m" && 
         HISTORies.ACTION == "Checked In Work Order" && 
         HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year 
         orderby 
         HISTORies.DATE 
         select new 
         { 
          HISTORies.WO, 
          SSes.TITLE, 
          SSes.DESCRIPT, 
          SSCUSTOMs.CUSTNAME, 
          SSes.STAKER, 
          HISTORies.USER, 
          SSes.STATUS, 
          HISTORies.OLDSTATUS, 
          HISTORies.NEWSTATUS, 
          CURRENT_STATUS = StatusTables.Description, 
          OLD_STATUS = StatusTable_1.Description, 
          NEW_STATUS = StatusTable_2.Description, 
          HISTORies.DATE.Value.Month, 
          HISTORies.DATE 
         }).Distinct(); 
     return SSQuery.ToList(); 
    }   
} 
+0

錯誤在最後一行。 SSQuery.ToList(); – CesarD

回答

3

新的實例在你LINQ你這樣做:

select new { 
          HISTORies.WO, 
          SSes.TITLE, 
          SSes.DESCRIPT, 
          SSCUSTOMs.CUSTNAME, 
          SSes.STAKER, 
          HISTORies.USER, 
          SSes.STATUS, 
          HISTORies.OLDSTATUS, 
          HISTORies.NEWSTATUS, 
          CURRENT_STATUS = StatusTables.Description, 
          OLD_STATUS = StatusTable_1.Description, 
          NEW_STATUS = StatusTable_2.Description, 
          HISTORies.DATE.Value.Month, 
          HISTORies.DATE 
         } 

這是一個動態類型,所以它不會匹配你的返回類型List<SSQuerySchema>

嘗試初始化您在Linq中指定的類型並設置屬性當然。

select new SSQuerySchema { 
           // initialize all properties here 
          } 
+0

如果使用初始化程序,則需要顯式指定屬性名稱。 – Kenneth

+0

@Kenneth yup,你是對的懶惰複製粘貼 – shenku

+0

這工作完美,謝謝。 – CesarD

1

你試圖返回IList<SSQuerySchema>,但實際的返回類型爲IList<dynamic>(你不選擇SSQuerySchema的,但一個匿名類型。

您應該做的函數的返回類型IList<dynamic>或修改您的選擇創建的SSQuerySchema