Asp.net mvc 从多个父类型检索子类型并将其关联

Asp.net mvc 从多个父类型检索子类型并将其关联,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,最有效的方法是什么 a从多个父类型检索所有子对象,以及 b知道父项类型是什么,以及每个子项的确切父项Id吗 目前,这就是我正在做的,而且效率非常低,至少在我找到每个孩子的特定父母的部分 public class ChildModel { public int Id { get; set; } public string Name { get; set; } } public class ParentType1Model { public int Id { get; se

最有效的方法是什么

a从多个父类型检索所有子对象,以及

b知道父项类型是什么,以及每个子项的确切父项Id吗

目前,这就是我正在做的,而且效率非常低,至少在我找到每个孩子的特定父母的部分

public class ChildModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ParentType1Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ChildModel> Children { get; set; }
}

public class ParentType2Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ChildModel> Children { get; set; }
}

//Get all ChildModels from ParentType1
var parentType1Children = db.ParentType1Models
    .SelectMany(x => x.Children)
    .ToList();

listOfChildModels.AddRange(parentType1Children);

//Get all ChildModels from ParentType2
var parentType2Children = db.ParentType2Models
    .SelectMany(x => x.Children)
    .ToList();

listOfChildModels.AddRange(parentType2Children);


//Find the parent for each ChildModel
foreach (var child in listOfChildModels)
{
    ParentType1Model parentType1ModelCheck = null;
    ParentType2Model parentType2ModelCheck = null;

    parentType1ModelCheck = await db.ParentType1Models 
        .Where(p => p.Children
        .Any(i => i.Id == child.Id))
        .FirstOrDefaultAsync();

    //If first check is null, then move to second check
    if (taskProjectModelCheck == null)
    {
        parentType2ModelCheck = await db.ParentType2Models 
            .Where(p => p.Children
            .Any(i => i.Id == child.Id))
            .FirstOrDefaultAsync();
    }

    //Now record the parent type and parent Id in an object containing the original ChildModel and it's parent's info (to be used later for various things)
    ChildViewModel childViewModel = new ChildViewModel();
    childViewModel.ChildModel = child;
    if (parentType1ModelCheck != null)
    {
        childViewModel.ParentType = "ParentType1";
        childViewModel.ParentModelId = parentType1ModelCheck.Id;
    }

    else if (parentType2ModelCheck != null)
    {
        childViewModel.ParentType = "ParentType2";
        childViewModel.ParentModelId = parentType2ModelCheck.Id;
    }           
}   

像这样的怎么样

var ids1 = from p in db.ParentType1Models
            from c in p.Children
            select new
            {
                parentId = p.Id,
                parentName = p.Name,
                childName = c.Name,
                childId = c.Id,
                ParentType = "One"
            };


var ids2 = from p in db.ParentType2Models
            from c in p.Children
            select new
            {
                parentId = p.Id,
                parentName = p.Name,
                childName = c.Name,
                childId = c.Id,
                ParentType = "Two"
            };

var results = ids1.Union(ids2).ToList();

我最终使用了原始SQL,速度非常快。 通过直接针对数据库编写查询,我可以直接转到Entity Framework在设置ParentTypeXModels和ChildModels时创建的多对多关系表

result = dbContext.Database.SqlQuery<ANewChildObject>(
"select 
    ParentModelId = pm.Id, 
    Id = c.Id, 
    ParentType = 'ParentType1'  
from dbo.ChildModels c 
    JOIN dbo.ParentType1ModelsChildModels pmT ON c.Id = pmT.ChildModel_Id 
    JOIN dbo.ParentType1Models pm on pmT.ParentType1Model_Id = pm.Id 

UNION ALL

 select 
    ParentModelId = pm.Id, 
    Id = c.Id, 
    ParentType = 'ParentType2'  
from dbo.ChildModels c 
    JOIN dbo.ParentType2ModelsChildModels pmT ON c.Id = pmT.ChildModel_Id 
    JOIN dbo.ParentType2Models pm on pmT.ParentType2Model_Id = pm.Id" 
).ToList();

这简化并缩短了代码,但最终增加了大约3倍的响应时间。@amartin这很不幸。我包括了演示的名字。也许只限制ID会允许它只命中索引。如果看不到查询计划,很难知道。我相信你是对的,但不幸的是,我需要来自ChildModels的很多属性。