我有一个 SqlServer 函数,该函数根据输入执行 cte 递归选择,输入是带有 ids 的 csv 字符串。
不幸的是,我不能在我的函数中使用“option(maxrecursion 0)”,它必须在函数执行时使用。问题是我找不到如何将此选项与 EntityFramework 的 EntitySql 一起使用。
考虑到我的函数被调用MyRecursiveFunction
,这里有一些代码片段:
public virtual IQueryable<MyFunctionReturnType> ExecuteMyFunction(IObjectContextAdapter objContextAdapter, string csvIds)
{
var idsParam = new ObjectParameter("idsParam", csvIds);
// This is the original one, that works, but has no "option(maxrecursion 0)"
return objContextAdapter.CreateQuery<MyFunctionReturnType>("[MyRecursiveFunction](@idsParam)", idsParam);
// gives me an error of incorrect syntax near "option"
return objContextAdapter.CreateQuery<MyFunctionReturnType>("select VALUE tblAlias from [MyRecursiveFunction](@idsParam) as tblAlias OPTION(MAXRECURSION 0)", idsParam);
// Also gives me syntax error:
return objContextAdapter.CreateQuery<MyFunctionReturnType>("MyRecursiveFunction(@idsParam) option(maxrecursion 0)", idsParam);
}
任何人都知道如何option(maxrecursion 0)
与 entitySql 一起使用?
我知道我可以使用“ExecuteStoreQuery”来执行我想要的任何 sql 查询,但我确实需要一个 IQueryable,因为“ExecuteMyFunction”的这个返回将在实现之前与另一个 IQueryable 连接。
请节省您的时间,不要建议ExecuteStoreQuery
与AsQueryable
...一起调用。我真的不想实现整个结果集,因为我只会实现 10 个结果用于 paging。
这是我的 TVF 的表示:
-- Consider that I have the given table for executing this function.
-- This table has a foreign key to itself, as the data represents a tree, like an organization chart
CREATE TABLE MyTable
(
Id INT,
ParentId INT, -- FK to 'MyTable'.'Id',
Name VARCHAR(400)
)
-- Here is my function definition:
CREATE FUNCTION MyRecursiveFunction (@idsParam VARCHAR(MAX))
RETURNS TABLE
AS
RETURN
(
-- create a cte for recursively getting the data
with myCte (id, parentId) as
(
SELECT tbl.Id, tbl.ParentId FROM MyTable AS tbl
-- This function just transform the varchar into a table of "Value"
INNER JOIN [dbo].[SplitTextIntoTableOfInt](@idsParam, ',') AS ids ON a.ParentId = ids.Value
UNION ALL
SELECT a.Id, a.ParentId FROM myCte AS parent
INNER JOIN MyTable tbl ON tbl.ParentId = parent.Id
)
SELECT * FROM myCte -- I can't use 'option(maxrecursion 0)' in here
)