Dixin / EntityFramework.Functions

EntityFramework.Functions library implements Entity Framework code first support for stored procedures (with single result type, multiple result types, output parameter), table-valued functions (returning entity type, complex type), scalar-valued functions (composable, non-composable), aggregate functions, built-in functions, niladic functions, and model defined functions.
https://weblogs.asp.net/Dixin/EntityFramework.Functions
MIT License
79 stars 27 forks source link

Cannot use TableValuedFunction within LINQ to Entities query #13

Open mniemirka opened 7 years ago

mniemirka commented 7 years ago

When method marked as FunctionType.TableValuedFunction is used within IQueryable linq method chain, exception: 'The specified method XXX on the type YYY cannot be translated into a LINQ to Entities store expression.' is thrown.

Dixin commented 7 years ago

@mniemirka Can you share your code so that I can look into?

micahbright commented 7 years ago

I have the same problem. I am using 1.4.0. Basically, I'm trying to use my function as a subquery. My LINQ to Entities query:

        _db.Things
            .Where(d => MyGuids.Contains(d.Guid.Value))
            .SelectMany(c => _db.fnMyFunction(c.Value)).Distinct()
            });

Where MyFunction is like this:

  [Function(FunctionType.TableValuedFunction, nameof(fnMyFunction), "MyModel", Schema = "dbo")]
    public IQueryable<MyReturnType> fnMyFunction(
        [Parameter(DbType = "uniqueidentifier", Name = "Value")]Guid Value,
        )
    {
        ObjectParameter valueParameter = new ObjectParameter("Value", Value);
        return this.ObjectContext().CreateQuery<MyReturnType>(
            $"[dbo].[{nameof(fnMyFunction)}](@Value)", valueParameter);
    }
ahocquet commented 6 years ago

Same issue here

osnoser1 commented 6 years ago

Same problem... 😪

osnoser1 commented 6 years ago

Fixed! I use in namespaceName the value nameof(MyContext).

SmittyTan commented 5 years ago

Hello guys, I am still having this issue. Here is my snippet, can you please point me on the right direction using EF 6.1.3

--------------database function---------------------


CREATE FUNCTION [dbo].[Split]
(   
      @Input NVARCHAR(MAX),
      @Delimiter CHAR(1)
)
RETURNS @Output TABLE (
         ID int IDENTITY(1,1) PRIMARY KEY,
      Item VARCHAR(100) --PRIMARY KEY
)
AS
BEGIN
……
END
RETURN

------------inside the context ------------------

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {         
            modelBuilder.Conventions.Add(new FunctionConvention<CurriculumContext>());           
            modelBuilder.Types<Keyword>();
}

[Function(FunctionType.TableValuedFunction, nameof(Split), nameof(CurriculumContext), Schema = "dbo")]
        public IQueryable<Keyword> Split(string input, string delimiter)
        {
            List<ObjectParameter> parameters = new List<ObjectParameter>();
            parameters.Add(new ObjectParameter("input", input));
            parameters.Add(new ObjectParameter("delimiter", "delimiter"));
            string functionStr = string.Format("[{0}].{1}", GetType().Name, "[Split](@input, @delimiter)");           
            return this.ObjectContext().CreateQuery<Keyword>(functionStr, parameters.ToArray());
        }

inside the service

            var assessments = _unitOfWork.RepositoryFor<Assessment>()
                .AllIncluding(a => a.TeacherAssessmentLinks)
                .Where(a => _unitOfWork.Split("adaptive", ",").Where(b => a.Title == b.Item).Count() > 0);

-------- exeption  occurred right here-----
var test1 = assessment.ToList()

exceptions: INQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Curriculum.Core.Domain.Keyword] Split(System.String, System.String)' method, and this method cannot be translated into a store expression

If I call it directly it worked: var test = _unitOfWork.Split("hello,world", ",").Where(a => a.Item == "hello"); test1 = test.ToList();

Please advise and thank you in advance!

SmittyTan commented 5 years ago

osnoser1 - I am wondering if you could help me out here. thank you so much in advance!

SmittyTan commented 5 years ago

@Dixin, I am wondering if you know what is the cause of this issue. Thanks in advance!

Smitty