linq到实体:无效父母的模式

发布于 2025-01-21 11:46:44 字数 1209 浏览 2 评论 0原文

我正在查询一个实体框架模型,因此在这种情况下,我将LINQ用于实体。这只是意味着我无法使用所有最新的操作员。

这是一个Linq选择语句:

            var nodes = r.Find(pn =>
                pn.StatusId == PageStatus.Active &&
                pn.IncludeInSitemap &&
                sitemap.Contains(pn.PageTemplate.SiteSectionId))
                .Select(x => new
                {
                    x.PageTemplate.RouteName,
                    x.RouteValues,
                    x.PageTemplate.PhysicalFile,
                    MetaLastModified = x.PageMeta.LastModified,
                    ContentBlock = x.PageContent != null
                        ? x.PageContent.ContentBlock
                        : default,
                    ContentLastModified = x.PageContent != null
                        ? x.PageContent.LastModified
                        : default,
                    x.PageType.Priority
                })
                .OrderByDescending(x => x.Priority)
                .ThenBy(x => x.RouteName)
                .ToList();

如您所见,我有两个属性,需要检查PageContent并非零。我无法使用“?”操作员是因为它是实体的linq。但是,我想知道是否有可能对父进行零检查(而不是两个)并相应地设置两个属性。

Linq对我来说有点陌生,因此这可能是一个非常简单的答案或不可能的。

任何建议都感谢。

I'm querying an Entity Framework model, so In this case, I'm using LINQ to Entities. It just means I can't use all of the latest operators.

Here's a LINQ select statement:

            var nodes = r.Find(pn =>
                pn.StatusId == PageStatus.Active &&
                pn.IncludeInSitemap &&
                sitemap.Contains(pn.PageTemplate.SiteSectionId))
                .Select(x => new
                {
                    x.PageTemplate.RouteName,
                    x.RouteValues,
                    x.PageTemplate.PhysicalFile,
                    MetaLastModified = x.PageMeta.LastModified,
                    ContentBlock = x.PageContent != null
                        ? x.PageContent.ContentBlock
                        : default,
                    ContentLastModified = x.PageContent != null
                        ? x.PageContent.LastModified
                        : default,
                    x.PageType.Priority
                })
                .OrderByDescending(x => x.Priority)
                .ThenBy(x => x.RouteName)
                .ToList();

As you can see, I have two properties where I need to check PageContent is not null. I can't make use of the "?" operator because it is LINQ to Entities. However, I wondered if it is possible to do the NULL check on the parent once (instead of the two) and set both properties accordingly.

LINQ is a bit alien to me, so it could be a very simple answer or impossible.

Any advice appreciated.

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

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

发布评论

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

评论(1

挥剑断情 2025-01-28 11:46:44

这样的东西?

                .Select(x => new
                {
                    x.PageTemplate.RouteName,
                    x.RouteValues,
                    x.PageTemplate.PhysicalFile,
                    MetaLastModified = x.PageMeta.LastModified,
                    PageContent = x.PageContent
                    x.PageType.Priority
                })
             .Select(anonymousClass => 
                         anonymousClass.PageContent is PageContentType pg ? 
                                  new { ... } : 
                                  new {})    

如果我们用真实类替换匿名类...那么构造函数可以进行一次测试,您需要确保正确设置推论属性...

something like this ?

                .Select(x => new
                {
                    x.PageTemplate.RouteName,
                    x.RouteValues,
                    x.PageTemplate.PhysicalFile,
                    MetaLastModified = x.PageMeta.LastModified,
                    PageContent = x.PageContent
                    x.PageType.Priority
                })
             .Select(anonymousClass => 
                         anonymousClass.PageContent is PageContentType pg ? 
                                  new { ... } : 
                                  new {})    

if we replace the anonymous class by a real class... then the constructor can have the one time test you want to assure the deduced properties are correctly set...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文