LINQ 投影到表示模型

发布于 2024-08-19 10:26:06 字数 475 浏览 4 评论 0原文

我对 LINQ 和许多现代数据驱动应用程序设计技术非常陌生,所以这可能是一个非常基本的问题。

我正在尝试创建几个不同实体框架实体到简单表示模型的投影。假设我有实体 Parent(属性为 ID、Name、Age)和 Child(属性为 ID、Name、Age,并引用 Parent)。我想将它们投影到PresentationParent和PresentationChild,其中所有属性都是相同的,但PresentationParent有一个List。我该如何在 LINQ 中执行此操作?

from p in entities.Parent
select new PresentationParent
{
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = [[?? What goes here ??]]
}

这到底是在正确的轨道上吗?我似乎只能找到简单、平面投影的例子。

I'm pretty new to LINQ and a lot of modern data-driven application design techniques in general, so this may be a pretty basic question.

I'm trying to create a projection of a couple different Entity Framework entities to a simple presentation model. Let's say I have the entites Parent (properties are ID, Name, Age) and Child (properties are ID, Name, Age, with a reference to a Parent). I want to project these to PresentationParent and PresentationChild, where all the properties are the same, but PresentationParent has a List. How would I do this in LINQ?

from p in entities.Parent
select new PresentationParent
{
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = [[?? What goes here ??]]
}

Is this on the right track at all? I can only seem to find examples of simple, flat projections.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2024-08-26 10:26:06

像这样的事情:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = (from c in entities.Child
                where c.Parent == p
                select new PresentationChild {
                    ID = c.ID,
                    Name = c.Name,
                    Age = c.Age
                }).ToList()
}

但是,您的实体应该已经预先配置了必要的外键关系,因此您可以执行以下操作:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = p.Children.ToList()
}

当然,这将返回每个子项的所有属性,因此您可能想要投影子项反正:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = (from c in p.Children
                select new PresentationChild {
                    ID = c.ID,
                    Name = c.Name,
                    Age = c.Age
                }).ToList()
}

Something like this:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = (from c in entities.Child
                where c.Parent == p
                select new PresentationChild {
                    ID = c.ID,
                    Name = c.Name,
                    Age = c.Age
                }).ToList()
}

However, your entities should come pre-configured with the necessary foreign key relationships already, so you could do something like this:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = p.Children.ToList()
}

Of course, that would return all the properties of each child, so you might want to project the children anyway:

from p in entities.Parent
select new PresentationParent {
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = (from c in p.Children
                select new PresentationChild {
                    ID = c.ID,
                    Name = c.Name,
                    Age = c.Age
                }).ToList()
}
放低过去 2024-08-26 10:26:06

另一种选择,如果关系没有设置得足以让访问器可用:

from p in entities.Parent
from c in entities.Children on p.ID equals c.parentID into children
select new PresentationParent
{
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = children.ToList()
}

Another alternative, if the relationship isn't set up enough to have the accessor available:

from p in entities.Parent
from c in entities.Children on p.ID equals c.parentID into children
select new PresentationParent
{
    ID = p.ID,
    Name = p.Name,
    Age = p.Age,
    Children = children.ToList()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文