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?
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.
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.
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.
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.
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.
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.
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.
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.
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}");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With