CompiledQueries Possible中的If语句
本文关键字:语句 If 中的 Possible CompiledQueries | 更新日期: 2023-09-27 18:29:16
基本上,我想用基于标志的不同参数执行相同的查询。例如,当我的标志为真时,情况如下:-
private static Func<DataClassesDataContext, int, string, IQueryable<DAL.ViewType>> GetFuncComponentsByLibID =
CompiledQuery.Compile((DataClassesDataContext dCtx, int libID, string searchText) =>
(from k in dCtx.ViewTypeAttributes where (from r in dCtx.ViewTypeAttributes
where r.LibraryID == libID &&
r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null
select r.TypeID).Contains(k.TypeID)
select k));
如果我的标志为false,我希望在不使用libID参数的情况下执行相同的查询。
private static Func<DataClassesDataContext, int, string, IQueryable<DAL.ViewType>> GetFuncComponentsByLibID =
CompiledQuery.Compile((DataClassesDataContext dCtx, int libID, string searchText) =>
(from k in dCtx.ViewTypeAttributes where (from r in dCtx.ViewTypeAttributes
where r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null
select r.TypeID).Contains(k.TypeID)
select k));
最简单的方法是用标志"或"定义您的标准。
... (!flag || r.LibraryID == libID) ...
若标志为false,则比较值将为true。
这也是可能的:
private static Func<DataClassesDataContext, int, string, IQueryable<DAL.ViewType>> GetFuncComponentsByLibID =
CompiledQuery.Compile((DataClassesDataContext dCtx, int libID, string searchText) => {
if(flag)
return (from k in dCtx.ViewTypeAttributes where (from r in dCtx.ViewTypeAttributes
where r.LibraryID == libID &&
r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null
select r.TypeID).Contains(k.TypeID)
select k);
else
return (from k in dCtx.ViewTypeAttributes where (from r in dCtx.ViewTypeAttributes
where r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null
select r.TypeID).Contains(k.TypeID)
select k);
});
还有一个:
private static Func<DataClassesDataContext, int, string, IQueryable<DAL.ViewType>> GetFuncComponentsByLibID =
CompiledQuery.Compile((DataClassesDataContext dCtx, int libID, string searchText) => {
Func<ViewTypeAttributes, bool> whereFunc;
if(flag)
whereFunc = new Func<ViewTypeAttributes, bool>(r => r.LibraryID == libID &&
r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null);
else
whereFunc = new Func<ViewTypeAttributes, bool>(r => r.TypeAttributeValue.Contains(searchText) &&
r.TypeAttrStatus == null);
return (from k in dCtx.ViewTypeAttributes where (from r in dCtx.ViewTypeAttributes.Where(whereFunc)
select r.TypeID).Contains(k.TypeID)
select k);
});