Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Unable to create a constant value of type x. Only primitive types are supported in this context.

I have the following

A Engineer Model:

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

I fill the a list of engineers with the correct details:

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

So far so good.

My question how can I pull the engineers name to 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've 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'm 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 kinda of understand but cannot figure out away just to select the engineer name.

Thanks in advance

like image 246
user1153881 Avatar asked Jan 17 '12 12:01

user1153881


1 Answers

You should be able to simplify the first one to:

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

Having said that, Entity Framework probably won't let you run that when running a database call. If you can make a foreign key from ticket to engineer, then you can select it in the same way as you do the client company name. If not, then you'll need to do it in two goes: first, run the select without populating the engineer name property, and after that, fill them in with something like:

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

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

like image 157
harriyott Avatar answered Sep 22 '22 04:09

harriyott