实体框架 - 无法创建类型x的恒定值。只有原始类型都是在这个环境支持。 [英] Entity Framework - Unable to create a constant value of type x. Only primitive types are supported in this context.

查看:82
本文介绍了实体框架 - 无法创建类型x的恒定值。只有原始类型都是在这个环境支持。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下

一个工程师型号:

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

到目前为止好。

我的问题我怎么能拉工程师的名字下面的工程项:

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

                    }; 

我也试过

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.

在此先感谢

推荐答案

您应该能够简化第一个:

You should be able to simplify the first one to:

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

话虽如此,实体框架可能不会让你运行运行的数据库调用时。如果你可以从票工程师外键,那么你可以以同样的方式为你做客户的公司名称选择它。如果没有,那么你需要做的两个云:首先,运行不选择填充工程师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)

显然,你需要添加中的engineerID财产,并选择它的第一步。

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

这篇关于实体框架 - 无法创建类型x的恒定值。只有原始类型都是在这个环境支持。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆