-
Notifications
You must be signed in to change notification settings - Fork 547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aggregate functions with multiple parameters #2090
Comments
This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information. |
Implemented |
Hi Kemsky I need Implement STRING_AGG in EF6 and I see your comments, but I don't understand, can you write more information. Thanks |
@joseJositoJosete, it is quite complicated, due to multiple issues with EF6. You need EntityFramework.Functions package or implement your own convention.
Implement and register IDbCommandTreeInterceptor :
Implement visitor:
|
Hi I have some questions, can you help me.i implement your code and send me an error in this line GroupBy(l => new { l.IdExpediente }).Select(a => new { a.Key.IdExpediente, dd= a.Select(s => s.Investigador.Apellido1).StringAgg() }); "The specified method 'System.String StringAgg(System.Collections.Generic.IEnumerable`1[System.String])' on the type 'ISCIII.AESEG.DAL.AggregateFunctions' cannot be translated into a LINQ to Entities store expression.'" I think its becauese even OnModelCreating doestn execute, its because i dont use code first , i use edmx file. In your code you wrote two types "DbFunctionAggregate" and "DbExpressionListType" I don't have these types, and I write "CollectionType" and "AggregateFunctions" , its OK
Thanks |
@joseJositoJosete, I can not help you with Edmx, I have never used it. |
Don't worry you helped me :) |
@joseJositoJosete Were you able to make this work with an EDMX model? |
After much trial and error, I got it to work. I had to add a manual
Then I had to make a static function, note the
Then I used the interception that kemsky posted above to inject the second parameter since EF doesn't allow multi-parameter aggregates. Hopefully this saves someone else 8 hours. |
Thanks, you are a Good programer :).
I resolved create a view in sql server , but your
Idea is better . I dont like views.
Maybe you have a prolem. If you chage database and update edmx , you will lose the chages.
Thanks for all :).
…________________________________
De: MrZander ***@***.***>
Enviado: miércoles, 30 de octubre de 2024 22:41
Para: dotnet/ef6 ***@***.***>
Cc: joseJositoJosete ***@***.***>; Mention ***@***.***>
Asunto: Re: [dotnet/ef6] Aggregate functions with multiple parameters (Issue #2090)
After much trial and error, I got it to work.
I had to add a manual Function definition to the edmx file, in the SSDL section.
<Function Name="STRING_AGG" ReturnType="varchar(max)" Aggregate="true" BuiltIn="true" NiladicFunction="false" IsComposable="true" StoreFunctionName="STRING_AGG">
<Parameter Name="expression" Type="Collection(nvarchar(max))" />
</Function>
Then I had to make a static function, note the .Store in the namespace:
[DbFunction("MyEFModel.Store", "STRING_AGG")]
public static string StringAggregateComma(IEnumerable<string> stringvalues)
{
throw new NotSupportedException("Direct calls are not supported");
}
Then I used the interception that kemsky posted above to inject the second parameter since EF doesn't allow multi-parameter aggregates.
I had to make a minor change with the ", " parameter to force it to be an varchar instead of nvarchar, since our DB only uses varchar.
Also, one last note, if you have any other interceptors, put this one last. EF throws an error complaining about too many arguments if another command tree interceptor visits the tree after this one.
Hopefully this saves someone else 8 hours.
—
Reply to this email directly, view it on GitHub<#2090 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BHWZN5MJ6SRBYR3VFILW6ILZ6FG63AVCNFSM6AAAAABQ26OAVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBYGQ3DSNRVHE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
This is a follow up for #85 [Add ability to have custom aggregate functions with multiple parameters].
It became obvious that it was not fully implemented, specifically, I tried to make builtin STRING_AGG function work. This function takes additional argument - separator.
It turns out that
System.Data.Entity.Core.Query.PlanCompiler.Normalizer.VisitCollectionAggregateFunction
unconditionally drops all argument nodes except the first one (code reference).I've tried to pass remaining child nodes to the
CreateNode
method and it worked just fine:So this is one-line fix that enables using
STRING_AGG
which is super useful. I can try to create a PR if possible.Example:
The text was updated successfully, but these errors were encountered: