C# 如何在 Linq 中仅对选定的列使用 INCLUDE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9031054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:40:54  来源:igfitidea点击:

How to use INCLUDE in Linq for selected columns only?

c#sql-serverlinqincludeentity-framework-4.1

提问by Red Swan

I am having the scenario with multiple linq include methods with table object associations. Suppose the scenario is:

我有多个 linq include 方法与表对象关联的场景。假设场景是:

User has Groups
User has Permissions
User has Vehicles

var _users=
(from u in dbconetxt.Users
join g in dbconetxt.Gropus on u.userId equals g.userId
join p in dbconetxt.Permissions on u.userId equals p.userId
join v in dbconetxt.Vehicles on u.userId equals v.userId
Where u.Status=true
select u).Include(u.Groups)
         .Include(u.Permissions)
         .Include(u.Vehicles)
         .ToList()

After joining all these tables within a single query, I select the User Object. Certainly, I would get List, but I want each User Object should Include its respective Groups, Permissions, Vehicles, but from Vehicles and Permissions, I want to load only few Columns/Properties, not all. So, how do I specify which Columns to load in this scenario?

在单个查询中加入所有这些表后,我选择用户对象。当然,我会得到列表,但我希望每个用户对象都应该包括其各自的组、权限、车辆,但是从车辆和权限中,我只想加载少数列/属性,而不是全部。那么,如何指定在这种情况下加载哪些列?

I am using Entity Framework 4.1 + C# + SQL Server.

我正在使用实体框架 4.1 + C# + SQL Server。

采纳答案by Kralizek

Include is an extension method attached to IQueryable. That means you have to use it on the DbSet Users.

Include 是附加到 IQueryable 的扩展方法。这意味着您必须在 DbSet 用户上使用它。

If you want to select only specified columns, you can create an anonymous type or a class with a parameterless constructor.

如果只想选择指定的列,可以创建匿名类型或具有无参数构造函数的类。

var users = from u in dbContext.Users.Include(u => u.Groups)
            where u.Status == true
            select new 
            {
                u.Name,
                u.Id,
                u.WhatSoEver
            };

or

或者

var users = from u in dbContext.Users.Include(u => u.Groups)
            where u.Status == true
            select new UserView
            {
                Name = u.Name,
                Id = u.Id,
                Property1 = u.WhatSoEver
            };

回答by Ladislav Mrnka

If you want subset of columns you must use projection to anonymous or custom type or create specific database view for your query and map it to new read only entity. Includewill always load all columns of included entity (so other approach is dividing the entity with table splitting but that looks like overkill). Also Includehas very limited usage - shape of the root query mustn't change so once you use any custom projection or join Includewill not work.

如果您想要列的子集,您必须使用投影到匿名或自定义类型,或者为您的查询创建特定的数据库视图并将其映射到新的只读实体。Include将始终加载包含实体的所有列(因此其他方法是使用表拆分来划分实体,但这看起来有点矫枉过正)。使用也Include非常有限 - 根查询的形状不得更改,因此一旦您使用任何自定义投影或连接Include将不起作用。

EF is ORM - once you map entity you will always work with whole entity not only its part. If you need to work with part of the entity you must use projection to non entity class (non mapped).

EF 是 ORM - 一旦您映射实体,您将始终与整个实体一起工作,而不仅仅是它的一部分。如果您需要处理实体的一部分,您必须使用投影到非实体类(非映射)。