实体框架查询中的错误(缺少强制转换)

时间:2013-04-06 02:05:22

标签: c# sql entity-framework

public List<ProjectImpacts> getProjectImpactsByProjeactIDAndImpactName(String prefe , String impcName)
{ 
    String xim =  cecbContext.Impacts.First(i=>i.impt_name.Contains(impcName)).impt_reference;

    IQueryable<ProjectImpacts> query = from c in cecbContext.ProjectImpacts
            join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference             
            where c.proj_reference == prefe && c.impt_reference == xim
            select b.impt_name;

    List<ProjectImpacts> SelectedImpacts = query.ToList(); //query.Select(refe => new ProjectImpacts { impt_reference =   }).ToList();

    return SelectedImpacts;
}

我在此查询中收到错误:

  

无法将类型'System.Linq.IQueryable'隐式转换为'System.Linq.IQueryable'。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:3)

这是因为您的查询最后选择了一个名称:

IQueryable<ProjectImpacts> query = from c in cecbContext.ProjectImpacts
    join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference             
    where c.proj_reference == prefe && c.impt_reference == xim
    // select b.impt_name; // <<== Replace this...
    select c;              // <<== with this.

通用T的类型参数IQueryable<T>对应于查询中选择的对象的类型。由于您选择了name(可能是string),因此您获得了IQueriable<string>。选择c ProjectImpacts后,您将获得IQueryable<ProjectImpacts>作为结果