我在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類文件中沒有錯誤,我使用相同的查詢來檢索另一個項目中的記錄,它運行良好,不知道爲什麼它在這裏不起作用。
我不明白..你的返回類型是'的IList'和你的變量是'的IList '...他們顯得格格不入 –
您應該返回的IEnumerable''(或至少'ICollection ''),除非您需要通過其索引真正訪問項目。而且,即使如此,您仍然可以在接收枚舉項的代碼段中調用「ToList()」。 '列表',甚至'IList '都不推薦作爲返回值。 –
而且,你的LINQ函數沒有意義......你正在對一個新的匿名對象執行一個'GroupBy' ......它們都不會被分組。然後,你在'ToList()'之後就沒有必要了。所有你真正需要的是先執行'Where()'語句,然後執行'Select()'語句...這是最有效的方法。 –