c# - 为什么 EF 在执行 "from"连接时丢失包含数据

标签 c# linq entity-framework linq-to-entities

如果我有这样的 LINQ 表达式来提取用户表和相关数据。此查询按预期为我提供了填充对象图:

    var query = from u in Context.Users
                    .Include("EventRegistrations")
                    .Include("State")
                select u;

但是,如果我添加另一个相关的导航属性作为“来源”,即使我不对它做任何事情,我也只会在结果中得到单个对象数据,而没有包含数据。

    var query = from u in Context.Users
                    .Include("EventRegistrations")
                    .Include("State")
                from ur in u.UserRoles
                select u;

为什么会这样?我想在上面的表达式中的 where 子句中使用“ur”,但它使我无法获取包含的表数据。

最佳答案

这可能是因为你没有包含 UserRoles

var query = from u in Context.Users
                .Include("EventRegistrations")
                .Include("State")
                .Include("UserRoles")
            from ur in u.UserRoles
            select u;

关于c# - 为什么 EF 在执行 "from"连接时丢失包含数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270394/

相关文章:

c# - 如何在嵌套 ListView 中编辑数据

c# - 索引器属性的 Linq 表达式

c# - 从不同的 IEnumerable 映射 KeyValuePair

c# - 将带有动画元素的 C# winforms Panel 或 PictureBox 保存到 gif 文件?

c# - 如何优雅地关闭异步服务器套接字? C#

c# - 实体 - 两个连接表是什么类型

c# - 为什么 orderBy 不适用于此 linq 表达式?

c# - 为什么我不能在 C# 中将 json 字符串发送到服务器?

c# - LINQ to SQL查询将两列相乘并计算SUM

c# - Linq:如何按最大项目数分组