Entity Framework - It is not possible to create a constant value of type x. In this context, only primitive types are supported.

I have the following

Engineer Model:

public class engineers { public Guid? Guid { get; set; } public string Name { get; set; } } 

I fill out a list of engineers with the right details:

 List<engineers> listeng = new List<engineers>(); listeng.Add(new engineers { Name = profile.FirstName + " " + profile.LastName, Guid = GuidEngineer }); 

So far so good.

My question is: how can I pull the engineer name onto the eng entry below:

  var tickets = from o in new HelpdeskEntities().Tickets.Where(t => t.TicketState.State == "Open") select new AjaxTickets { TicketID = o.TicketID, TicketSubject = o.TicketSubject, ClientCompanyName = o.ClientCompany.ClientCompanyName, DateOpened = o.DateOpened, **eng** = list.Where(x => x.Guid == o.EngineerID).Select(x => new engineers {Guid = x.Guid, Name=x.Name }).FirstOrDefault().Name }; 

I also tried

 var tickets = from o in new HelpdeskEntities().Tickets.Where(t => t.TicketState.State == "Open") select new AjaxTickets { TicketID = o.TicketID, TicketSubject = o.TicketSubject, ClientCompanyName = o.ClientCompany.ClientCompanyName, DateOpened = o.DateOpened, **eng** = list.Where(x => x.Guid == o.EngineerID).Select(x => x.Name }).FirstOrDefault() }; 

The error I am getting is:

 Unable to create a constant value of type 'Helpdesk2.ViewModel.engineers'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."} 

Which I understand, but I canโ€™t understand, just choose the name of the engineer.

Thanks in advance

+6
source share
1 answer

You should be able to simplify the first option:

 list.FirstOrDefault(x => x.Guid == o.EngineerID).Name 

Having said that, the Entity Framework will probably not let you run this when starting a database call. If you can make a foreign key from the ticket to the engineer, you can choose it in the same way as the name of the client company. If not, then you need to do this twice: first run select without filling in the property of the engineerโ€™s name, and then fill them with something like:

 tickets.ForEach(ticket => ticket.EngineerName = engineers.First(eng => eng.Guid == ticket.EngineerID).Name) 

Obviously, you need to add the EngineerID property and select it in the first step.

+3
source

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


All Articles