简体   繁体   English

实体或复杂类型''不能在LINQ to Entities查询,三表外部联接中构造

[英]The entity or complex type '' cannot be constructed in a LINQ to Entities query, three table outer join

I see some related questions on stack, but not sure how to still solve it for my problem as I'm doing a three table left outer join. 我在堆栈上看到了一些相关的问题,但是由于正在做一个三表左外部联接,所以不确定如何解决我的问题。 RoleUser and Role are an entity object RoleUserRole是一个实体对象

var query = from u in Context.Users
            join ru in Context.RoleUsers on u.Id equals ru.UserId into plv
            from x in plv.DefaultIfEmpty(new RoleUser())
            join r in Context.Roles on x.RoleId equals r.Id into pii
            from y in pii.DefaultIfEmpty(new Role())
            orderby u.Id, y.Id
            select new UserWithRole() { Id = u.Id, User = u, Role = y };

As the error explains you cannot create in a linq query they mapped types ( RoleUser and Role ). 如错误所述,您无法在linq查询中创建它们映射的类型( RoleUserRole )。 Instead use the DefaultIfEmpty() . 而是使用DefaultIfEmpty() Something along the following: 以下内容:

var roles = from ru in Context.RoleUsers 
            join r in Context.Roles on ru.RoleId equals r.Id
            select new { ru.UserId, role = r };

var query = from u in Context.Users
            join r in roles on u.Id equals r.UserId into plv
            from x in plv.DefaultIfEmpty()
            select new UserWithRole() { Id = u.Id, User = u, Role = x.role };

However I'd suggest look into navigation properties - that way you will not have to construct the joins and work with it as if it is "normal" objects. 但是,我建议您研究一下导航属性-这样,您就不必构造联接并像对待“普通”对象一样使用它。

The problem is DefaultIfEmpty(new RoleUser()) and DefaultIfEmpty(new Role()) calls, because EF cannot translate creation of a constant entity object to SQL. 问题是DefaultIfEmpty(new RoleUser())DefaultIfEmpty(new Role())调用,因为EF无法将常量实体对象的创建转换为SQL。

So you need to use the parameterless overload of DefaultIfEmpty() and deal with null objects in the materialized query result. 因此,您需要使用DefaultIfEmpty()的无参数重载,并在物化查询结果中处理null对象。

If you don't need a IQueryable<T> result, you can use intermediate anonymous type projection, which to LINQ to Objects context by using AsEnumerable() and do the final desired projection: 如果不需要IQueryable<T>结果,则可以使用中间匿名类型投影,该对象通过使用AsEnumerable()到LINQ to Objects上下文进行LINQ并执行最终所需的投影:

var query = (from u in Context.Users
             join ru in Context.RoleUsers on u.Id equals ru.UserId into userRoles
             from ru in userRoles.DefaultIfEmpty()
             join r in Context.Roles on ru.RoleId equals r.Id into roles
             from r in roles.DefaultIfEmpty()
             orderby u.Id, r.Id
             select new { User = u, Role = r }
            )
            .AsEnumerable() // db query ends here
            .Select(x => new UserWithRole
            {
                Id = x.User.Id,
                User = x.User,
                Role = x.Role ?? new Role() // now this is supported
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq To Entities中的左外部联接(不能在LINQ to Entities查询中构造实体或复杂类型。) - Left outer join in Linq To Entities (The entity or complex type cannot be constructed in a LINQ to Entities query.) 实体或复杂类型&#39;x&#39;不能在linq to实体查询中构造 - the entity or complex type 'x' cannot be constructed in a linq to entities query 实体或 &gt; 复杂类型“ChildAccessory”不能在 LINQ 中构造到 &gt; 实体查询 - The entity or > complex type 'ChildAccessory' cannot be constructed in a LINQ to > Entities query 无法在 LINQ to Entities 查询中构造实体或复杂类型“ ” - The entity or complex type ' ' cannot be constructed in a LINQ to Entities query C#-无法在LINQ to Entities查询中构造实体或复杂类型 - C# - The entity or complex type cannot be constructed in a LINQ to Entities query 实体或复杂类型……无法在LINQ to Entities查询中构造 - The entity or complex type … cannot be constructed in a LINQ to Entities query 收到错误:无法在LINQ to Entities查询中构造实体或复杂类型 - Getting error:The entity or complex type cannot be constructed in a LINQ to Entities query 实体或复杂类型不能在LINQ to Entities查询中构造 - The entity or complex type cannot be constructed in a LINQ to Entities query “Controler中的实体或复杂类型不能在LINQ to Entities查询中构造” - “The entity or complex type cannot be constructed in a LINQ to Entities query” in Controler 实体或复杂类型“ xxx”不能在LINQ to Entities查询中构造吗? - The entity or complex type 'xxx' cannot be constructed in a LINQ to Entities query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM