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

Model-defined functions #3

Closed SteveRuble closed 8 years ago

SteveRuble commented 8 years ago

I figured out a way to implement model-defined functions, so you can write something like this:

[ModelDefinedFunction(nameof(FormatName), "EntityFramework.Functions.Tests.Examples",
@"CASE 
    WHEN person.Title IS NOT NULL THEN person.Title + ' ' 
    ELSE ''
  END + person.FirstName + ' ' + person.LastName"
)]
public static string FormatName(this Person person) => 
                                 string.Format("{0}{1} {2}",
                                               person.Title == null ? "" : person.Title + " ",
                                               person.FirstName,
                                               person.LastName);

If you call the method in a LINQ query, it gets translated to SQL, but if you call it on an actual instance of Person it executes the method. (Or you could have the implementation throw if you only want the function to work on the SQL side, of course.)

Would you be interested in a pull request for this?

Dixin commented 8 years ago

Absolutely. This is cool.