linq到实体:无效父母的模式
我正在查询一个实体框架模型,因此在这种情况下,我将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的东西?
如果我们用真实类替换匿名类...那么构造函数可以进行一次测试,您需要确保正确设置推论属性...
something like this ?
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...