[Proposal] LinQ to entities inline functions
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
I have this code:
```VB.NET
cmbProject.AddItems((
From b In db.Projects
Let name = b.Name.ToLower
Where b.FinishedDate Is Nothing AndAlso (
name.StartsWith(Text) OrElse
name.StartsWith("..." & Text) OrElse
name.Contains(" " & Text) OrElse
name.Contains(" ..." & Text) OrElse
name.StartsWith("(" & Text) OrElse
name.StartsWith("(..." & Text) OrElse
name.Contains(" (" & Text) OrElse
name.Contains(" (..." & Text)
)
Select b
Take 20
).ToList.Select(Function(r) (CInt(r.ID), r.DisplayName))
)
```
The part
```VB.NET
(
name.StartsWith(Text) OrElse
name.StartsWith("..." & Text) OrElse
name.Contains(" " & Text) OrElse
name.Contains(" ..." & Text) OrElse
name.StartsWith("(" & Text) OrElse
name.StartsWith("(..." & Text) OrElse
name.Contains(" (" & Text) OrElse
name.Contains(" (..." & Text)
)
```
is a criteria that I am using to select some names from the table. In fact I can complicate it more, but I am conceding performance.
I can't use Regex (despite it will be compact) because it can't be translated to SQL.
Worest, I need to repeat this criteria conditions with many other tables in different LinQ queries (about 10 queries). This is a lot of repeating that will make it hard to modify the criteria afterwards.
The obvious solution is to but it in a function like this:
```VB.NET
Function Filter(name As String, text As String) As Boolean
Return name.StartsWith(text) OrElse
name.StartsWith("..." & text) OrElse
name.Contains(" " & text) OrElse
name.Contains(" ..." & text) OrElse
name.StartsWith("(" & text) OrElse
name.StartsWith("(..." & text) OrElse
name.Contains(" (" & text) OrElse
name.Contains(" (..." & text)
End Function
```
which will reduce the linq query to:
```VB.NET
cmbProject.AddItems((
From b In db.Projects
Where b.FinishedDate Is Nothing AndAlso Filter(b.Name.ToLower, Text)
Take 20
).ToList.Select(Function(r) (CInt(r.ID), r.DisplayName))
)
```
Put this gives rruntime exception, because LimQ to Entities can't convert the Filter function to SQL!
I tried to mark the Filter function with the
``
attribute, but still got the same error!
So, it is obvious that the Filter function should can be considered as a part of the LinQ statement, and VB should inline it and translate its code to the expression tree.
So, my suggestion, is that VB should checl the AggressiveInlining case when it is related to a function used in LinQ, so, the inlining happens at the VB compiler's level (not the JIT level) before generating the expression trees.
Ir will be better if we use another attribute for this matter to avoid any confusion, say:
``
There will be rule on the code written in LinQ inlined functions so it can be converted to SQL, such as using only supported .NET methods.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.