我有以下實體框架 - 無法創建類型x的常量值。在此上下文中僅支持基本類型。
一個工程師型號:
public class engineers
{
public Guid? Guid { get; set; }
public string Name { get; set; }
}
我填用正確的方式與一個工程師的名單:
List<engineers> listeng = new List<engineers>();
listeng.Add(new engineers { Name = profile.FirstName + " " + profile.LastName, Guid = GuidEngineer });
到目前爲止好。
我的問題我怎麼可以拉工程師的名字下面的工程項:
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
};
我也試過
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()
};
我得到的錯誤是:
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."}
我有點理解,但不能弄清楚只是選擇工程師的名字。
在此先感謝
你不能在Linq-to-entities查詢中使用你的'list'。只能使用數據庫中存在的數據。您不能任意組合linq-to-objects和linq-to-entities,因爲EF不知道如何將其轉換爲SQL。從數據庫加載票據後,使用@ harriyott的解決方法並填充'Name'。 – 2012-01-17 13:30:25