Entity Framework Core LINQ Tree Expression Problem with Concat

.net-core c# entity-framework-core expression-trees

Question

As a result, this block of code accomplishes a bit less than my IQueryable extension. In essence, I'm merging a number of strings before applying a Contains to them. Entity Framework Core doesn't support System.String, which is my current issue. Concat performs the query's relevant portion locally, which is clearly not what I want.

Here is the little loop I used to combine these several strings:

                List<Expression> stringExpressionsToConcat = new List<Expression>();
                foreach (var member in memberExpressions)
                {
                    stringExpressionsToConcat.Add(member);
                    stringExpressionsToConcat.Add(spaceConstant);
                }
                //call the concat to create the final term to search on
                NewArrayExpression arrayExpression = Expression.NewArrayInit(typeof(string), stringExpressionsToConcat);
                searchStringExpression = Expression.Call(concatMethod, arrayExpression);

Although it is currently compiling for Entity to SQL, this is client-side functionality. I had the identical issue with an Order By clause while I was doing the following:

.ThenByDescending(e => string.Concat(e.FirstName, " ", e.LastName))

Since this is precisely what I'm constructing in my expression tree, it is evident that this did not transfer to Entity to SQL either. But if you do it like this...

.ThenByDescending(e => e.FirstName + " " + e.LastName)

does not convert an entity to SQL. Therefore, I'm curious as to how I may produce the identical expression that is shown in the code above and is appropriately delivered to SQL. I've experimented with Expression. In the expression builder, add is not supported for string type objects. Is this achievable, or does Entity Framework have some additional logic that enables it? I've tried looking through the source code on GitHub, but it's difficult to pinpoint the specific location of the issue.

1
2
3/11/2020 5:51:58 PM

Accepted Answer

Aha! I know how to do it now! The data is sent to SQL via this expression tree and it displays the same behavior!

Below is modified code:

//more than one member has the property, we need to combine them with spaces in between
List<Expression> stringExpressionsToConcat = new List<Expression>();
foreach (var member in memberExpressions)
{
    stringExpressionsToConcat.Add(member);
    stringExpressionsToConcat.Add(spaceConstant);
}

searchStringExpression = stringExpressionsToConcat[0];
for (int i = 1; i < stringExpressionsToConcat.Count; i++)
{
    searchStringExpression = Expression.Add(searchStringExpression, 
    stringExpressionsToConcat[i], typeof(string).GetMethod("Concat", new[] { 
    typeof(string), typeof(string) }));
}

The warning that Entity Framework sent in version 2.2 (and the error in version 3.1) were converted into the SQL code below.

([e].[MemberExpression1] + N' ') + [e].[MemberExpression2]) + N' ') + [e].[MemberExpression3]) + N' ')

This is precisely how Entity Framework generates the Order By-related SQL phrase that I mentioned in my response! Although the logic behind it is still a mystery to me, if you're wanting to make your own expression trees that combine strings so that the Entity Framework Core tree visitor can convert them into SQL, this is it!

I must give credit to this response since it led me in the right direction: https://stackoverflow.com/a/3858421/5245385

2
3/11/2020 7:50:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow