简体   繁体   English

如何在Linq中仅为选定列使用INCLUDE?

[英]How to use INCLUDE in Linq for selected columns only?

I am having the scenario with multiple linq include methods with table object associations. 我正在使用具有表对象关联的多个linq include方法的场景。 Suppose the scenario is: 假设场景是:

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. 当然,我会得到List,但我希望每个用户对象都应包含其各自的组,权限,车辆,但是来自车辆和权限,我只想加载少数列/属性,而不是全部。 So, how do I specify which Columns to load in this scenario? 那么,如何指定在此方案中加载哪些列?

I am using Entity Framework 4.1 + C# + SQL Server. 我正在使用Entity Framework 4.1 + C#+ SQL Server。

Include is an extension method attached to IQueryable. Include是附加到IQueryable的扩展方法。 That means you have to use it on the DbSet Users. 这意味着你必须在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
            };

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. 如果需要列的子集,则必须使用投影到匿名或自定义类型,或者为查询创建特定的数据库视图,并将其映射到新的只读实体。 Include will always load all columns of included entity (so other approach is dividing the entity with table splitting but that looks like overkill). Include将始终加载所包含实体的所有列(因此其他方法是将实体与表拆分划分,但看起来有点过分)。 Also Include has very limited usage - shape of the root query mustn't change so once you use any custom projection or join Include will not work. 此外, Include用法非常有限 - 根查询的形状不得更改,因此一旦您使用任何自定义投影或加入, Include将无效。

EF is ORM - once you map entity you will always work with whole entity not only its part. EF是ORM - 一旦你映射实体,你将始终与整个实体合作,而不仅仅是它的一部分。 If you need to work with part of the entity you must use projection to non entity class (non mapped). 如果需要使用实体的一部分,则必须使用投影到非实体类(非映射)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM