实体或复杂类型...不能在LINQ to Entities查询中构造 [英] The entity or complex type ... cannot be constructed in a LINQ to Entities query

查看:336
本文介绍了实体或复杂类型...不能在LINQ to Entities查询中构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一种方法有效,而另一种却不起作用,当它们似乎都在做相同的事情时,即构造一个实体.那么我的问题是,有没有一种方法可以在L2E查询中构造实体,而不必只使用Linq或两者都使用?

Why does one method work but not the other, when it seems they are both doing the same thing, i.e. constructing an entity. My question then, is there a way to construct the entity in a L2E query instead of having to use just Linq or indeed both?

这很好...

var queryToList = (from ac in ctx.AuthorisationChecks
                   where wedNumbers.Contains(ac.WedNo)
                   orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                   select new AuthorisationCheck
                   {
                       Blah = ac.Blah
                   }).ToList();

model.AuthorisationChecks = queryToList.Select(x => new AuthorisationCheck
    {
        Blah = x.Blah
    }).ToList();

但是,如果我改变...

However, if i change...

var queryToList

model.AuthorisationChecks queryToList // Of type List<AuthorisationCheck>

我得到标题中的错误...

i get the error in the Title...

The entity or complex type 'Model.AuthorisationCheck' cannot be constructed in a LINQ to Entities query.

在模型中,这简直就是无济于事.

In the model it is simply, nothing fancy here.

public List<AuthorisationCheck> AuthorisationChecks { get; set; }

对此进行了一些整理(效果很好)...

Tidied this up a little to be (which works fine)...

model.AuthorisationChecks = (from ac in ctx.AuthorisationChecks
                             where wedNumbers.Contains(ac.WedNo)
                             orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                             select ac).ToList()
                             .Select(x => new AuthorisationCheck
                             {
                                 Blah = x.Blah
                             }).ToList();

我的解决方案 我对匿名类型方法不满意,因此继续创建了一个简单模型,该模型仅包含我需要在视图模型中使用的属性.

My Solution I wasn't happy with the anonymous type method and so went ahead and created a simple model containing only the properties I required to be used in the viewmodel.

更改了模型的类型.AuthorisationChecks

Changed the type of model.AuthorisationChecks

来自

List<AuthorisationCheck> // List of Entities

List<AuthorisationCheckModel> // List of models

它允许以下代码工作,并且无需进行概要分析,似乎比使用匿名类型要快得多(当然,我不会两次强制转换为列表!).

which allows the following code to work, and without profiling it seems a lot quicker than using an anonymous type (and of course I don't cast to a list twice!).

model.AuthorisationChecks = (from ac in ctx.AuthorisationChecks
                             where wedNumbers.Contains(ac.WedNo)
                             orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                             select new AuthorisationCheckModel
                             {
                                 Blah = x.Blah
                             }).ToList();

P.S.曾经有一位同事(曾为Microsoft工作)警告过我,以这种方式直接使用Entities不是一个好主意,也许这就是他在想的原因之一,我也注意到了一些奇怪的地方.在其他情况下(主要是损坏)直接使用实体进行行为.

P.S. I was once warned by a coworker (who used to work for Microsoft) that it isn't a good idea to directly use Entities in this manner, maybe this was one of the reasons he was thinking of, I've also noticed some odd behavior using Entities directly in other cases (mainly corruptions).

推荐答案

如果此查询工作正常:

var queryToList = (from ac in ctx.AuthorisationChecks
                   where wedNumbers.Contains(ac.WedNo)
                   orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                   select new AuthorisationCheck
                   {
                       Blah = ac.Blah
                   }).ToList();

然后这也应该起作用:

model.AuthorisationChecks = (from ac in ctx.AuthorisationChecks
                   where wedNumbers.Contains(ac.WedNo)
                   orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                   select new AuthorisationCheck
                   {
                       Blah = ac.Blah
                   }).ToList();

在第一种情况下,您无需再次进行投影,可以将其直接分配给模型属性:

and in your first case you don't need to project again, you can directly assign it to model propeerty:

model.AuthorisationChecks = queryToList;

更新:

由于是Linq To Entities,因此您必须使用匿名类型执行以下操作:

UPDATE:

As it is Linq To Entities,you have to do something like this using anonymous type:

var queryToList = (from ac in ctx.AuthorisationChecks
                       where wedNumbers.Contains(ac.WedNo)
                       orderby ac.WedNo, ac.ExpAuthDate, ac.ActAuthDate
                       select new 
                       {
                           Blah = ac.Blah
                       }).ToList();

然后:

model.AuthorisationChecks = queryToList.Select(x => new AuthorisationCheck
    {
        Blah = x.Blah
    }).ToList();

这篇关于实体或复杂类型...不能在LINQ to Entities查询中构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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