16

While using EF (up to version 6.1.3 at least) assuming you have a class like this:

 class Customer
 {
      public string FirstName { get; set; }
      public string LastName { get; set; }
 }

if you to get a field FullName that is the concatenation of both (FirstName and LastName) as a field in query result you would have to do something like this:

db.Customers.Select(c => new { FullName = c.FirstName + " " + c.LastName })

now that there is String Interpolation in C# could you do something like this instead

db.Customers.Select(c => new { FullName = $"{c.FirstName} {c.LastName}" })

this might seem like a trivial example (which it is) but the question remains.

Can I use this out of the box, do I need to make some tricks to get it working or is it sure it won't work?

4
  • 6
    Umm... why don't you just, you know, try it? You already have all you need to check it out for yourself. Just do it. Commented Feb 2, 2016 at 16:12
  • This really comes down to if EF handles string.Format as that's what string interpolation is turned into. Commented Feb 2, 2016 at 16:13
  • I have just stumbled upon this article where they explain some configurations in order to do what i want Commented Feb 2, 2016 at 16:43
  • 1
    @Luiso Ugh, that article literally shows you how to use string interpolation to create sql that is susceptible to sql injection attacks, not the same as using it in EF that would try to translate code to SQL. Commented Mar 24, 2017 at 19:48

2 Answers 2

23

I wouldn't expect so, no. It'll compile down to a string.Format call, which I wouldn't expect to be supported. If you really need the projection to be done in the SQL part, you could test it... but otherwise, as normal, use AsEnumerable() when you've finished the part of the query you need to be performed in the database, and then use Select after that:

var query = db.Customers
              // Project to just the properties we need
              .Select(c => new { c.FirstName, c.LastName })
              // Perform the rest of the query in-process
              .AsEnumerable()
              .Select(c => $"{c.FirstName} {c.LastName}");
Sign up to request clarification or add additional context in comments.

Comments

12

could you do something like this instead

Not in the general sense, because string interpolation is just translated to a string.Format call, replacing the placeholders with numbers and passing the values as parameters. So your format gets translated from

$"{c.FirstName} {c.LastName}" 

to

string.Format("{0} {1}", c.FirstName, c.LastName);

Since not all functionality of string.Format() (custom format strings, padding, justification, etc) can be directly translated to SQL, it is not supported.

do I need to make some tricks to get it working or is it sure it won't work?

I doubt there are any tricks you can do to get it to work in Ling-to-SQL since you are dealing with string.Format internally. You could retrieve all of the pieces you need, call AsEnumerable to change the context from Linq-to-SQL to Linq-to-Objects, and then use interpolation in a subsequent projection, but in this trivial case using string concatenation is cleaner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.