LINQ 查询中的 WHERE 子句似乎失败
请原谅任何天真,我是 C# 世界的新手。如果我遗漏了有用的信息,请告诉我。
我正在为 Dynamics CRM 2011 的客户门户构建自定义 SiteMapProvider。首先,我初始化一个数组:
public Adx_webpage[] WebPages;
如下所示填充该数组:
public MyProvider()
{
WebPages = (FROM p in CrmContext.Adx_webpageSet WHERE p.Adx_HiddenFromSitemap != true SELECT p).ToArray();
}
后来,我尝试像这样查询 WebPages[]:
Adx_webpage[] childPages = (FROM p in WebPages WHERE p.adx_parentpageid.Id == page.Id SELECT p).ToArray();
当我通过调试器运行此命令时,我得到一个NullReferenceException 指向我的 WHERE 子句中的条件,表示 p.adx_parentpageid.Id 为 null,这对于网站的主页来说是正确的。这就引出了一个问题:
为什么这个查询会在我的查询中将主页显示为 p
?我有什么误解吗?
Please forgive any naivety, I'm brand new to the world of C#. Let me know if I've left out helpful information.
I'm building a custom SiteMapProvider for the Customer Portal for Dynamics CRM 2011. First I initialize an array:
public Adx_webpage[] WebPages;
which gets populated thusly:
public MyProvider()
{
WebPages = (FROM p in CrmContext.Adx_webpageSet WHERE p.Adx_HiddenFromSitemap != true SELECT p).ToArray();
}
Later, I try to query WebPages[] like so:
Adx_webpage[] childPages = (FROM p in WebPages WHERE p.adx_parentpageid.Id == page.Id SELECT p).ToArray();
When I run this through the debugger, I get a NullReferenceException which points at the condition in my WHERE clause saying that p.adx_parentpageid.Id is null, which is true for the home page of the site. Which leads to the question:
Why would this query turn up the home page as a p
in my query? What am I misunderstanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的第一行将
返回您站点地图中未隐藏的所有页面。但这也包括没有父 ID 的主页。因此,当您的第二个查询枚举此集合时,它将尝试访问此为空的属性并抛出空引用异常。您只需要在查询中满足这一点即可。
Your first line
Will return you all pages that are not hidden in your site map. But this is also including your home page which has no parent id. So when your second query enumerates over this collection it is going to attempt to access this property which is null and throw a null reference exception. You just need to cater for this in your query.
问题是您第一次查询时的 .ToArray() 。此 linq 查询正在为您的 CrmContext 提供程序创建本机查询:
.ToArray() 导致 linq 查询立即运行并返回一个普通的旧对象数组。该数组未附加到您的 CrmContext 对象。
但是,此查询使用 Linq to Objects 来迭代第一次调用中返回的数组。它不会生成对您的 CrmContext 提供程序的本机查询。
它实际上与使用 foreach 循环迭代数组中的每个元素相同,因此您必须担心检查第一个查询返回的可能为空值。
The issue is the .ToArray() on you first query. This linq query is creating a native query to your CrmContext provider:
The .ToArray() is causing the linq query to be run immediatly and returns a plain old array of objects. The array is not attached to your CrmContext object.
However, this query is using Linq to Objects to iterate over the array returned in the first call. It is not generateing a native query to your CrmContext provider.
Its effectively the same as using a foreach loop to iterate over each element in the array so you have to worry about checking for possible null values that were returned by your first query.
我不明白你的问题。您说主页是一个网页,并且它没有从站点地图中隐藏,那么为什么您希望主页在查询中显示为
p代码>?
无论如何,你可以简单地使用
p == null
跳过任何内容:I don't understand your question. You're saying that the homepage is a web page and it is NOT hidden from the sitemap, so why wouldn't you expect the homepage to show up in your query as a
p
?Anyway, you can simply skip anything with
p == null
: