2013-07-11 72 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