2

I have a User entity with FirstName and LastName because this is how a name is stored in the database, but I would like to return a DTO with the Full Name, that will be a computed property using both properties.

Currently, I'm doing it with this:

from u in myContext.Users
    select new  { FullName = $"{u.FirstName} {u.LastName}" };
  1. Does this code make big impact in the database, since the FullName property is calculated?

  2. Would it be better to have a calculated FullName property in my User entity? I would like to do it, but I don't know the disadvantages that could this have. For example, filtering using the computed property.

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • Define "disadvantages". That’s a very broad term. What bas thing you fear would happen? You are combining first and last name on the client side. What impact would that have on the DB? – Sefe Dec 06 '17 at 18:05
  • @Sefe for example, that due to the computed value, I'm afraid that the query would fetch all the records incurring in longer (in time) and resource consuming queries. – SuperJMN Dec 06 '17 at 18:07
  • Why not profile it using SQL Server Profiler? Are you expecting your interpolated string to be translated into an equivalent SQL expression? – SpruceMoose Dec 06 '17 at 18:12
  • 1
    _"due to the computed value, I'm afraid that the query would fetch all the records"_ - the query **will** fetch all the records, as that's what you have told the context to do, it's nothing to do with what you're doing afterwards. – stuartd Dec 06 '17 at 18:17
  • I don't know how to use the profiler. Moreover, it's not my server and I don't have one. I would like know whether the interpolation is translated or not. Will it be different if I use FullName = u.FirstName + " " + u.LastName? – SuperJMN Dec 06 '17 at 18:19
  • @stuartd I mean, fetched and loaded into memory in order be evaluated. – SuperJMN Dec 06 '17 at 18:20
  • I would expect `FullName = u.FirstName + " " + u.LastName` to be translated but not the interpolated string. Can't say for sure but that is what I have observed with EF Core recently. If you want to inspect the generated SQL without a profiler, you could check out [this post](https://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework#20751723). – SpruceMoose Dec 06 '17 at 18:28
  • @CalC Thanks! Do you know how to do that trick using ASP.NET Core? the context is created in each request automatically. – SuperJMN Dec 06 '17 at 19:42
  • 1
    Sorry, for some reason I completely missed that this was EF Core. You should be able to view the SQL from the (debug) Output Window in Visual Studio, see [Examine SQL sent to the database](https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/advanced#examine-sql-sent-to-the-database) – SpruceMoose Dec 06 '17 at 20:30

0 Answers0