我在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()'语句...这是最有效的方法。 –