Calling a SQL User-defined function in a LINQ query

后端 未结 2 1234
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 06:53

I am having a hard time getting this to work. I am trying to do a radius search using the following Filter helper on an IQueryable. There are a set of other filters that get

2条回答
  •  情歌与酒
    2020-12-01 07:28

    if you use Code-First approach, then you cannot call UDFs as you want (as of EF6) - here is the proof, and another one. You are only limited to calling UDF as a part of your SQL query:

    bool result = FooContext.CreateQuery(
        "SELECT VALUE FooModel.Store.UserDefinedFunction(@someParameter) FROM {1}",
        new ObjectParameter("someParameter", someParameter)
    ).First();
    

    which is ugly IMO and error-prone.

    Also - this MSDN page says:

    The process for calling a custom function requires three basic steps:

    1. Define a function in your conceptual model or declare a function in your storage model.

    which essentially means you need to use Model-First approach to call UDFs.

提交回复
热议问题