C# 无法隐式转换类型';System.Linq.IQueryable
我编写了以下编译查询:C# 无法隐式转换类型';System.Linq.IQueryable,c#,linq,C#,Linq,我编写了以下编译查询: public static Func < Modal.Entities, string, IQueryable < Modal.Staff > > MyQuery = CompiledQuery.Compile((Modal.Entities U, string StaffNo) => U.Staff.Where(a => a.StaffNo == StaffNo)); Modal.Staff abc = MyQuer
public static Func < Modal.Entities, string, IQueryable < Modal.Staff > >
MyQuery = CompiledQuery.Compile((Modal.Entities U, string StaffNo) => U.Staff.Where(a => a.StaffNo == StaffNo));
Modal.Staff abc = MyQuery(context, StaffNo);
但我得到了以下错误:
无法将类型“System.Linq.IQueryable”隐式转换为“Modal.Staff”。存在显式转换(是否缺少强制转换?)
有人能帮我吗?您的查询返回System.Linq.IQueryable,但您正在尝试将其分配给Modal.Staff。更改查询以返回单个结果:
MyQuery = CompiledQuery.Compile((Modal.Entities U, string StaffNo) =>
U.Staff.Where(a => a.StaffNo == StaffNo).FirstOrDefault());