2013-07-11 125 views
0

我在WPF中這樣做,我正在使用實體框架。隱式轉換類型

這是我的CRUD類文件我的查詢代碼:

private DAO.DAOQuestionHint qh = new DAO.DAOQuestionHint(); 

    public MainWindow2() 
    { 
     InitializeComponent(); 
     PopulateQuestion(1, 5); 
    } 

    private void PopulateQuestion(int activityID, int taskID) 
    { 


     IList<QuestionHint> lstQuestionHints = qh.GetListKeys(taskID, activityID); // ERROR 

     //codes here... 
     } 

我收到此錯誤代碼我xaml.cs的背後:

public class QuestionHint 
    { 
     public int? QuestionNo { get; set; } //change the type accordingly 
     public int? ActivityID { get; set; } //change the type accordingly 
     public int? TaskID { get; set; } //change the type accordingly 
     public string Answer { get; set; } //change the type accordingly 
     public string QuestionContent { get; set; } //change the type accordingly 
     public string joined { get; set; } //change the type accordingly 
     public string joinOption { get; set; } //change the type accordingly 

    } 

     public IList<QuestionHint> GetListKeys(int listTask, int listActivity) 
    { 
     IList<QuestionHint> lstRecords = context.questionhints.GroupBy(x => new { x.QuestionNo, x.ActivityID, x.TaskID }).ToList().Select(g => new QuestionHint() 
     { 
      QuestionNo = g.Key.QuestionNo, 
      ActivityID = g.Key.ActivityID, 
      TaskID = g.Key.TaskID, 
      joined = String.Join(" ", 
       g.OrderBy(q => q.questionhintID) 
       .Select(i => i.QuestionContent + "[" + i.Answer + "]")), 
      joinOption = String.Join(" ", 
        g.OrderBy(q => q.questionhintID) 
        .Select(a => "[" + a.Option1 + "," + a.Option2 + "]")) 


     }).Where(x => x.TaskID == listTask && x.ActivityID == listActivity) 
      //.Take(50) 
    .ToList(); 

     return lstRecords; 
    } 

我在後面的代碼調用此:

無法隱式轉換類型 'System.Collections.Generic.IList' 爲' System.Collections.Generic.IList」。有 顯式轉換存在(您是否缺少演員?)

iStellar是該項目的名稱。 DAOQuestionHint是CRUD類文件的名稱。

在CRUD類文件中沒有錯誤,我使用相同的查詢來檢索另一個項目中的記錄,它運行良好,不知道爲什麼它在這裏不起作用。

+2

我不明白..你的返回類型是'的IList '和你的變量是'的IList '...他們顯得格格不入 –

+0

您應該返回的IEnumerable''(或至少'ICollection ''),除非您需要通過其索引真正訪問項目。而且,即使如此,您仍然可以在接收枚舉項的代碼段中調用「ToList()」。 '列表',甚至'IList '都不推薦作爲返回值。 –

+0

而且,你的LINQ函數沒有意義......你正在對一個新的匿名對象執行一個'GroupBy' ......它們都不會被分組。然後,你在'ToList()'之後就沒有必要了。所有你真正需要的是先執行'Where()'語句,然後執行'Select()'語句...這是最有效的方法。 –

回答

2

您在每個示例中使用不同的大寫字母作爲泛型參數 - IList<QuestionHint> in GetListKeys() and IList<Model.questionhint> in PopulateQuestion()。我想這些指的是類似命名但不同類型。

+0

我注意到這一點,但我不能寫Model.questionHint – user2376998

+0

的QuestionHint istead @ user2376998如果QuestionHint是在一個名爲「模式」這很好的命名空間,但它必須是'Model.QuestionHint',不'Model.questionhint '。如果你右鍵點擊每一個,然後點擊'去定義',他們是否去同一個地方? – TheEvilPenguin

+0

Opps我誤解了QuestionHint,它實際上是一個單獨的類,請參閱上面的編輯。 – user2376998