Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use C# string interpolation with Linq to SQL

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?

like image 457
Luiso Avatar asked Feb 02 '16 16:02

Luiso


People also ask

Does anybody use C anymore?

There is at least one C compiler for almost every existent architecture. And nowadays, because of highly optimized binaries generated by modern compilers, it's not an easy task to improve on their output with hand written assembly.

What can I use C for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.

Should I use C++ or C?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

Can we use C language?

C is highly portable and is used for scripting system applications which form a major part of Windows, UNIX, and Linux operating system. C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc.

Should I use C++ for Stack Overflow?

With C++ you can have very long compile times (which means, of course, more time for Stack Overflow!). Show activity on this post. If you want your code to be understood by virtually any programmer write in C. Show activity on this post. I'm surprised no one's mentioned libraries.

Should I learn C++ or C?

Most compilers use name mangling, and the ones that don't do something at least as messy. If your system lives on its own, as is the case with many applications, then C++ is a fine choice. If your system needs to interact with software not neccesarily written in C++ (most frequently in assembler, or Fortran Libraries) then you are in a tight spot.

Can I use a C++ library in a C++ program?

C++ is pretty much the only thing that can use a C++ lib (defined as 'a lib that uses features in C++ that are not in C [such as overloaded functions, virtual methods, overloaded operators, ...], and does not export everything through C compatible interfaces via extern "C"'). Show activity on this post.

Should we switch from C++ to C?

If we were to go a step further and switch entirely to C we would gain little and lose the most useful constructs of C++. The biggest practical reason for preferring C is that support is more widespread than C++. There are many platforms, particularly embedded ones, that do not even have C++ compilers.


2 Answers

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}");
like image 83
Jon Skeet Avatar answered Oct 30 '22 15:10

Jon Skeet


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.

like image 37
D Stanley Avatar answered Oct 30 '22 16:10

D Stanley