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

本文关键字:转换 框架 查询 错误 实体 | 更新日期: 2023-09-27 18:07:35

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"。到System.Linq.IQueryable。存在显式转换(您是否缺少强制类型转换?)

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

这是因为您的查询在末尾选择了一个名称:

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.

泛型IQueryable<T>的类型参数T对应查询中选择的对象类型。由于您选择了name(可能是string),因此您得到了IQueriable<string>。一旦你选择了c,也就是ProjectImpacts,你会得到IQueryable<ProjectImpacts>作为你的结果