我有以下代码,我试图使用LINQ创建CompiledQuery。
public static Func<DataContext, int, object>
GetTeamMembersByTeamId = CompiledQuery.Compile<MazikReviewEntities, int, object>(
(context, teamId) => from tp in context.TeamPlayers
join t in context.Teams on tp.TeamId equals t.ID
join emp in context.Employees on tp.EmployeeId equals emp.ID
where tp.TeamId == teamId && emp.IsActive == true
orderby emp.JoiningDate
select new ProjTeamMemberDetails
{
EmployeeName = emp.FirstName + " " + emp.LastName,
Email = emp.Email,
Designation = context.Designations.FirstOrDefault(s => s.ID == emp.DesignationId).Name,
NIC = emp.NIC,
JoiningDate = emp.JoiningDate,
EmployeeID = emp.ID
});
}
在CompiledQuery.Compile行中,我的datacontext出现错误,指出
“类型'MazikGlobalReviewModel.MazikReviewEntities'不能在泛型类型或方法'System.Data.Linq.CompiledQuery.Compile(System.Linq.Expressions.Expression&gt;)'中用作类型参数'TArg0'。没有从'MazikGlobalReviewModel.MazikReviewEntities'到'System.Data.Linq.DataContext'的隐式引用转换。“
这是什么错误以及如何解决这个问题?
答案 0 :(得分:0)
我的朋友来自Mazik。如果你看一下错误就很清楚那里有什么问题。 看看下面的代码。你的代码中的datacontext和MazikReviewEntities是什么。确保它们属于同一类型。
public static readonly Func<**MyDataContext**, int, IQueryable<tblSomething>> CompiledSelectSites =
CompiledQuery.Compile((**MyDataContext** c, int conf_id) =>
(from s in c.TblSomething
where s.ID == id
select site));
有关详情请见link。
这可以解决您的问题。