Linq to Entities - NotSupportedException - dynamic type - string.Format


I make the transition from Linq-to-SQL to Linq-to-Entities and during the conversion I get the error "LINQ to Entities does not recognize the method." The format is System.String (System.String, System.Object, System.Object) ', and this method cannot be translated into a storage expression. "This code is the culprit. I understand that it is necessary to create a dynamic type that I use below. How can I use a dynamic type and use L2E?

var query = _db.Persons.Where(p => p.PersonId == PersonId);
if (query.Count() > 0)
{
  var data = query.Join(_db.Users, p => p.UserId, u => u.UserId, (p, u) => new
    {
      Id = p.PersonId,
      Name = string.Format("{0} {1}", p.FirstName, p.LastName),
      Phone = p.Phone,
      Email = u.Email
    }).Single();
  return data;
}

EDIT: Okay, I feel a little silly after @John Hartsock showed me a simple solution ...
What if I wanted to do some more complicated string manipulations?

+3
1

?

var query = _db.Persons.Where(p => p.PersonId == PersonId);
if (query.Count() > 0)
{
  var data = query.Join(_db.Users, p => p.UserId, u => u.UserId, (p, u) => new
    {
      Id = p.PersonId,
      Name = p.FirstName + " " + p.LastName,
      Phone = p.Phone,
      Email = u.Email
    }).Single();
  return data;
}

http://msdn.microsoft.com/en-us/library/cc716715.aspx

+5

Source: https://habr.com/ru/post/1776286/


All Articles