【问题标题】:Linq query to fill ViewModel collection including child ViewModel collectionsLinq 查询以填充 ViewModel 集合,包括子 ViewModel 集合
【发布时间】:2016-03-10 19:11:50
【问题描述】:

我正在尝试研究如何进行 Linq 查询,您可以在其中获取对象集合及其子视图模型集合。

假设您有这些模型:

public class Parent
{
    public int ParentId {get; set;}
    public string ParentName {get; set;}
    public virtual ICollection<FirstChild> FirstChildren { get; set; }
    public virtual ICollection<SecondChild> SecondChildren { get; set; }
}

public class FirstChild
{
    public int FirstChildId {get; set;}
    public int ParentId {get; set;}
    public string FirstChildName {get; set;}
}

public class SecondChild
{
    public int SecondChildId {get; set;}
    public int ParentId {get; set;}
    public string SecondChildName {get; set;}
}

然后你有一些视图模型:

public class ParentViewModel
{
    public int ParentId {get; set;}
    public string ParentName {get; set;}
    public virtual ICollection<FirstChild> FirstChildren { get; set; }
    public virtual ICollection<SecondChildViewModel> SecondChildren { get; set; }
}

public class SecondChildViewModel
{
    public int SecondChildId {get; set;}
    public int ParentId {get; set;}
    public string SecondChildName {get; set;}
}

是的,ParentViewModel 将有一个模型集合和一个视图模型集合。

我想做一个 linq 查询,最终填充一个 ParentViewModel 列表并填充子集合。

var result = (from p in db.Parent.Include(p => p.FirstChildren).Include(p => p.SecondChildren)
            select new ParentViewModel {
                ParentId = p.ParentId,
                ParentName = p.ParentName,
                FirstChildren = p.FirstChildren,
                SecondChildren = p.SecondChildren as SecondChildViewModel
            });

最好的处理方法是什么?

【问题讨论】:

  • 你的解决方案有什么问题(除了你不能投p.SecondChildren as SecondChildViewModel)?
  • p.SecondChildrenSecondChild 类型,我正在尝试将其分配给ParentViewModel.SecondChildren 类型SecondChildViewModel。我得到:'不能将类型'System.Collections.Generic.ICollection'隐式转换为'System.Collections.Generic.ICollection'。存在显式转换(您是否缺少演员表?)'

标签: c# linq


【解决方案1】:

如果这一行

SecondChildren = p.SecondChildren as SecondChildViewModel

是问题所在,你应该简单地处理它类似于Parent -> ParentViewModel,即使用投影:

SecondChildren = p.SecondChildren.Select(c => new SecondChildViewModel {
    SecondChildId = c.SecondChildId,
    ParentId  = c.ParentId,
    SecondChildName = c.SecondChildName
}).ToList()

【讨论】:

  • 如此简单,如此简单。 (捂脸)
【解决方案2】:

尝试在 Linq 中使用 dataloadoption。这是使用您的代码的示例:

var dataContext = new dataContext();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<FirstChildren>( p => p.FirstChildren);
dlo.LoadWith<SecondChildren>( p => p.SecondChildren);
dataContext.LoadOptions = dlo;

var result = (from p in dataContext.Parent
            select new ParentViewModel {
                ParentId = p.ParentId,
                ParentName = p.ParentName,
                FirstChildren = p.FirstChildren,
                SecondChildren = p.SecondChildren as SecondChildViewModel
            });

单击here 获取 Microsoft 的一个很好的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多