简体   繁体   English

在将include语句与实体框架一起使用时选择特定列

[英]select specific columns when using include statement with entity framework

When I need a hierarchal (parent-child) relationship, I typically use the Include statement in my EF query. 当我需要一个层级(父子)关系时,我通常在我的EF查询中使用Include语句。

Example: 例:

DbContext.Customers.Include("Projects");

This is fine, but the Customers and Projects entities always brings back all the columns. 这很好,但Customers和Projects实体总是带回所有列。

I know that the below query will bring back specific columns in the parent table, but I'm also trying to bring back only specific columns in the child table. 我知道下面的查询将带回父表中的特定列,但我也试图只返回子表中的特定列。 If I use the intellisense on the Projects it is obviously a collection and does not give specific properties to select. 如果我在Projects上使用intellisense,它显然是一个集合,并没有给出特定的属性来选择。

from c in Customers
let Projects = c.Projects.Where (p => p.Notes != null)
where Projects.Any()
select new
{
    c.UserName,
    Projects
}

I tried refining the query to the below code, but as you can see, the Projects entity is a child entity of the Customers and therefore, does not have a specific column to select in the query. 我试着炼查询到下面的代码,但你可以看到,该项目实体是客户的子实体 ,因此,没有一个特定的列在查询中进行选择。 It obviously is a collection. 它显然是一个集合。

Is there a way to bring back just specific columns in each of the entities when using an Include in your query? 在查询中使用Include时,是否有办法在每个实体中仅返回特定列

Note that my YeagerTechDB.ViewModels.Customers model is made up of all columns that reside in the Customer and Project entities. 请注意,我的YeagerTechDB.ViewModels.Customers模型由驻留在Customer和Project实体中的所有列组成。

public List<YeagerTechDB.ViewModels.Customers> GetCustomerProjects()
        {
            try
            {
                using (YeagerTech DbContext = new YeagerTech())
                {
                    var customer = DbContext.Customers.Include("Projects").Select(s =>
                        new YeagerTechDB.ViewModels.Customers()
                        {
                            CustomerID = s.CustomerID,
                            ProjectID = s.ProjectID,
                            UserName = s.UserName,
                            Name = s.Projects.,
                        });

                     return customer.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

ANSWER #1 FOR 1 CHILD ENTITY 对1个儿童实体的答案#1

from c in Customers
let Projects = c.Projects.Where (p => p.Notes != null)
where Projects.Any()
select new
{
    c.UserName,
    Projects
}

ANSWER #2 FOR 2 CHILD ENTITIES 对2个儿童实体的答案#2

from c in Customers
let highValueP =
    from p in c.Projects
    where p.Quote != null
    select new { p.ProjectID, p.Name, p.Quote }
where highValueP.Any()
from p in Projects
let highValuet =
    from t in p.TimeTrackings
    where t.Notes != null
    select new { t.ProjectID, t.Notes }
where highValuet.Any()
select new 
{
    c.CustomerID,
    Projects = highValueP,
    TimeTrackings = highValuet
}

Edit #3 编辑#3 在此输入图像描述

Check this link for more details. 查看此链接了解更多详情。 In short, the trick is to use .Select() and anonymous type to restrict the columns you want. 简而言之,诀窍是使用.Select()和匿名类型来限制所需的列。 In the example below first Select() is actually doing this: 在下面的示例中,首先Select()实际上是这样做的:

var results = context.Products
        .Include("ProductSubcategory")
        .Where(p => p.Name.Contains(searchTerm)
                    && p.DiscontinuedDate == null)
        .Select(p => new
                        {
                            p.ProductID,
                            ProductSubcategoryName = p.ProductSubcategory.Name,
                            p.Name,
                            p.StandardCost
                        })
        .AsEnumerable()
        .Select(p => new AutoCompleteData
                            {
                                Id = p.ProductID,
                                Text = BuildAutoCompleteText(p.Name,
                                    p.ProductSubcategoryName, p.StandardCost)
                            })
        .ToArray();

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

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