I've encountered yet another problem on my quest to learn Razor :-). I am trying to fetch the id of a page using some Where() clauses, but i cant get my code working. Here is what i have:
Error loading Razor Script [KV][Razor]Dokument.cshtml No property or field 'Id' exists in type 'Func`2'
I did some trial and error testing and found that it is the last Where("Id != Model.Id") which is causing the error. I tried some other appoaches but all failed, so i am pretty stuck at the moment.
Does anyone have any idea how to go around problem? Any help will be much appreciated
I've tested similar code and get the same error, seems like a bug:
@foreach (var page in Model.AncestorOrSelf(1).Children.Where("Id != Model.Id"))
{
<li>@page.Name</li>
}
For now what you could do is create a list of Id's and get is from there:
@{ var IdList = new List<int>(); foreach (var page in Model.AncestorOrSelf().Descendants().Where("rightContent")) { IdList.Add(page.Id); } <h1>@IdList.Where(x => x != Model.Id).Last()</h1> }
Good to see that you found an easier solution! Too bad that you can't mark your own answers as the solution (..yet! this is on the list of things to fix on "Our").
In cases like this, where the queries can get somewhat complex, it might be easier for you to cast the Model object to a DynamicNode. In that way, you'll be able to use "real linq" and avoid some of the oddities in using dynamic expressions. Following is just an example.
Problem comparing Id's within a Where()
Hi,
I've encountered yet another problem on my quest to learn Razor :-).
I am trying to fetch the id of a page using some Where() clauses, but i cant get my code working. Here is what i have:
This is the error i get:
Error loading Razor Script [KV][Razor]Dokument.cshtml
No property or field 'Id' exists in type 'Func`2'
I did some trial and error testing and found that it is the last Where("Id != Model.Id") which is causing the error.
I tried some other appoaches but all failed, so i am pretty stuck at the moment.
Does anyone have any idea how to go around problem?
Any help will be much appreciated
/Thor
Eureka! I managed to fix the issue myself.
@Model.Id just had to be "outside" of the quotes.
Here is how that looks:
/Thor
I've tested similar code and get the same error, seems like a bug:
For now what you could do is create a list of Id's and get is from there:
Thanks for the input Sebastian, I will try out your snippet :-)
Good to see that you found an easier solution! Too bad that you can't mark your own answers as the solution (..yet! this is on the list of things to fix on "Our").
In cases like this, where the queries can get somewhat complex, it might be easier for you to cast the Model object to a DynamicNode. In that way, you'll be able to use "real linq" and avoid some of the oddities in using dynamic expressions. Following is just an example.
Just a suggestion :)
is working on a reply...