1

I've built a custom aggregate

CREATE AGGREGATE Helpers.Median(@input REAL)
RETURNS REAL EXTERNAL NAME Aggregates.Median

Written this in edmx:

<Function Name="Median" Aggregate="true" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="Helpers" ReturnType="float">
  <Parameter Name="input" Type="Collection(float)" Mode="In" />
</Function>

Created this class:

public class MySqlFunctions
{
    [DbFunction("Entities.Store", "Median")]
    public static float Median(IEnumerable<float> arg)
    {
        throw new NotSupportedException("Direct calls are not supported.");
    }
}

Executed

    using (Entities context = new Entities ())
    {
        var q = context.DataTable
            .GroupBy(x => x.ID
            , y => y.Value
            , (k, g) => new { k, 
               c=MySqlFunctions.Median(g) }
            ).ToList();
    }

And got this error:

System.NotSupportedException: 'The specified method 'Single Median(System.Collections.Generic.IEnumerable`1[System.Single])' on the type 'em_PriceTrackerModel.Store.MySqlFunctions' cannot be translated into a LINQ to Entities store expression because its return type does not match the return type of the function specified by its DbFunction attribute.'

What did I do wrong?

user2820173
  • 308
  • 2
  • 13
  • please specify the Database and the Language (MySQL, C#?) – serge Oct 04 '18 at 13:10
  • I've added it to title and tags – user2820173 Oct 04 '18 at 13:12
  • have a look on this https://stackoverflow.com/a/42790289/961631 – serge Oct 04 '18 at 13:19
  • you don't need to put the tags both in the title and tags, just the tags is enough, good luck ! – serge Oct 04 '18 at 13:21
  • Serge, I think that issue is a little bit different. I'm trying to import a function and types doesn't match, meanwhile issue in that link was failing to find a specific function. Or should I apply a similar approach(like in the q) to convert SqlSingle <-> float? – user2820173 Oct 04 '18 at 14:06
  • I did my remarks as SO questions corrector, I have not enough knowledge of EDMX. I hope you will find your answer here. – serge Oct 04 '18 at 14:54

0 Answers0