gpt4 book ai didi

asp.net-mvc - Entity Framework - 无法创建类型为 x 的常量值。在此上下文中仅支持原始类型。

转载 作者:行者123 更新时间:2023-12-04 15:36:17 25 4
gpt4 key购买 nike

我有以下

工程师模型:

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 });

到现在为止还挺好。

我的问题是如何将工程师姓名拉到下面的 eng 条目中:
 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."}

我有点理解但无法弄清楚只是为了选择工程师名称。

提前致谢

最佳答案

您应该能够将第一个简化为:

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

话虽如此, Entity Framework 可能不会让您在运行数据库调用时运行它。如果你可以制作一个从票证到工程师的外键,那么你可以像选择客户公司名称一样选择它。如果没有,那么您需要分两步完成:首先,在不填充工程师姓名属性的情况下运行选择,然后用以下内容填充它们:
tickets.ForEach(ticket => ticket.EngineerName = engineers.First(eng => eng.Guid == ticket.EngineerID).Name)

显然,您需要添加 EngineerID 属性并在第一步中选择它。

关于asp.net-mvc - Entity Framework - 无法创建类型为 x 的常量值。在此上下文中仅支持原始类型。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8894715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com