基本上,我想基于标志用不同的参数执行相同的查询。 例如,以下是我的标志为真的情况: -
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));
答案 0 :(得分:1)
最简单的方法是用标志来标记你的标准。
... (!flag || r.LibraryID == libID) ...
如果flag为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);
});