A simple query:
(from user in _db.Users
select new UserObj
{
DisplayName = user.LastName + " " + user.FirstName
}).OrderBy(o => o.DisplayName)
.Where(w => w.EndDate == null)
.ToList();
The model class is:
public class UserObj
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string DisplayName {get; set;}
public DateTime? EndDate {get; set;}
}
However I got the warning
warn: Microsoft.EntityFrameworkCore.Query[20500] The LINQ expression 'where(new UserObj() {DisplayName = (([user].LastName + " " + [user.FirstName)}.EndDate == null)' could not be translated and will be evaluated locally.
Order of the statements is important. EF Core will only allow non-translatable expressions (*) , such as the string concatenation, in the last select statement.
So,
ToList()
)DisplayName
in your database which equals to LastName + ' ' + FirstName
and query this column in your custom DTO object.(*) things that EF Core not yet is able to translate into a similar server-side statement for the different data providers
Thanks for the comment by @NetMage. EndDate should be initialized. Added it the error is gone.
(from user in _db.Users
select new UserObj
{
DisplayName = user.LastName + " " + user.FirstName,
EndDate = user.EndDate
}).OrderBy(o => o.DisplayName)
.Where(w => w.EndDate == null)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.