3

I'm trying to return a new column that is the concatenation of id and name, with a hyphen inbeween, using dynamic-linq:

query.Select("new(id, name, id & " - " & name As idName")

But, I'm failing even to get simple concatenation working, even without the hyphen:

query.Select("new(id, name, id & name As idName")

This raises System.NotSupportedException

LINQ to Entities does not recognize the method System.String Concat(System.Object, System.Object) method, and this method cannot be translated into a store expression.

Is there any way around this, and if so, how would I also add the quoted section " - " to the expression in a way that dynamic-linq can interpret, also I'd like id and name to be variables as these change depending on the user's selection?

NOTE:

  • I'm using dynamic-linq because the names of the columns I want to concatenate vary, based on user selection, plus I'm already using dynamic-linq for a Where clause).
  • The id and name columns are both of type string
isedwards
  • 2,429
  • 21
  • 29

2 Answers2

3

The System.Linq.Dynamic NuGet package lists https://github.com/kahanu/System.Linq.Dynamic as the project page. Here, examples from Scott Guthrie's original 2008 blog on dynamic LINQ were being transferred over to the GitHub project's wiki, and the documentation was being extended.

However, looking through the issues list, the repository appears to be dead/dying (discussion here). Currently, the most active fork/NuGet package appears to be System.Linq.Dynamic.Core (which support .NET standard as well as .NET Core).

The documentation and examples in the original System.Linq.Dynamic are currently more extensive, but both wikis have a section on dynamic expressions and the expression language which include a section on operators. This includes concatenation:

x & y String concatenation. Operands may be of any type.

In my tests, concatenation works if you switch from the System.Linq.Dynamic NuGet package to the System.Linq.Dynamic.Core package (you don't have to be using .NET Core, it works with other versions of .NET as well).

Imports System.Linq.Dynamic.Core

query.Select("new(id & name As id_name)")
isedwards
  • 2,429
  • 21
  • 29
  • 1
    Correct, you can use + or & when concatenating strings. See this unit test : https://github.com/StefH/System.Linq.Dynamic.Core/blob/597127e53fc7f3ca0654a8ac105e9b98d690677b/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs#L1385 – Stef Heyenrath Feb 05 '18 at 21:19
2

If I understand you right this should work.

Imports System.Linq.Dynamic.Core

Dim col1 = "id"
Dim col2 = "name"

Dim results = query.Select($"new({col1} as {col1},
                                 {col2} as {col2},
                                 {col1} & "" - "" & {col2} as {col1}_{col2})")

There are maybe other ways, but you still will be only querying once the db, so it should be no problem I think.

Hope it helps...

isedwards
  • 2,429
  • 21
  • 29
Rafa Gomez
  • 690
  • 8
  • 24
  • this is close, but the names of my columns can vary. Is there any way that `.Id`, `.Name` and `.IdName` can also be changed dynamically based on the user's selection? (Can I somehow have interpolated strings on the left-hand side of the Anonymous Type)? – isedwards Jan 28 '18 at 19:53
  • What type is your query object? Are you using EF? – Rafa Gomez Jan 29 '18 at 10:51
  • Yes, it's an EF6 IQueryable. I'm using `Imports System.Linq.Dynamic` to get the `Select` overload that allows me to use a String as a selector. The error indicates that LINQ to Entities is being used, but I'm not sure that should make any difference. – isedwards Jan 29 '18 at 14:22