I've got a where clause that repeats a couple of times. I'd like to put it into a variable to make it easier to read and manage, but it throws an error when I try. Can anybody help?
Not sure I know the solution to your problem but could you please add the error message you receive as well? That will make it much easier for people to try and help you out :)
its also worth noting that the @ is not needed in-front of the whereClause as it is running within a code block already. So you only need .Where(whereClause)
That looks good. You could try something like this, I don't have the code in front of me to fully test it but I believe this should add each collection to a list of IEnumerable.
You could always do another lambda expression to get l2 pages. example.
List<IEnumerable<IPublishedContent>> whereClause = new List<IEnumerable<IPublishedContent>>();
//add level 1 content to list
whereClause.Add(L1page.Children.Where(x => x.IsVisible() && !excludedDocTypes.Contains(x.DocumentTypeAlias)));
//add level 2 content to list
whereClause.Add(whereClause.First().Where(x => x.Children.Where(y => y.IsVisible() && !excludedDocTypes.Contains(x.DocumentTypeAlias)));
not sure if that is useful at all, but may make code simpler.
The code may need some tweaking as again I don't have VS in front of me to test it. But that should add the first l1page.Children to the list, and I think the whereClause.First() should grab the the l1page.Children from the list and then allow you to then return result for the level 2 page to be added back to the list.
I am not sure the code can be nested like this though, so may be a case of moving the whereClause.First() out into another variable and then using that variable to grab the level 2 page content.
Using variable for strongly typed where clause
Hi,
I've got a where clause that repeats a couple of times. I'd like to put it into a variable to make it easier to read and manage, but it throws an error when I try. Can anybody help?
What I've got (working):
What I'd like:
The error I'm getting is:
Thanks
Hi M
Not sure I know the solution to your problem but could you please add the error message you receive as well? That will make it much easier for people to try and help you out :)
/Jan
Trying to do too many things at once! I've added the error message in the original post now.
Thanks
Hey M,
I don't believe that is possible, you are storing the where clause as a string, which will then be passed as a string inside the where().
This means the code is processing it like so:
rather then:
its also worth noting that the @ is not needed in-front of the whereClause as it is running within a code block already. So you only need .Where(whereClause)
Thanks for your reply. I thought that might be the case.
I'm trying a slightly different approach now.
This works for the top level, but I'd like to be able to change "L1page" to "L2page". I could do
But is there a more elegant way where I could just change the first bit? Maybe declare it as a list or something like that?
Hey M,
That looks good. You could try something like this, I don't have the code in front of me to fully test it but I believe this should add each collection to a list of IEnumerable.
Let me know if that helps/works :).
Thanks Jordan
Thanks Jordan. The problem I'm hitting now is L1page/L2page are out of context due to nesting.
Looks like I'm stuck redeclaring the variable at each level i.e.
Thanks for your help
Ah I see what you mean.
You could always do another lambda expression to get l2 pages. example.
not sure if that is useful at all, but may make code simpler.
The code may need some tweaking as again I don't have VS in front of me to test it. But that should add the first l1page.Children to the list, and I think the whereClause.First() should grab the the l1page.Children from the list and then allow you to then return result for the level 2 page to be added back to the list.
I am not sure the code can be nested like this though, so may be a case of moving the whereClause.First() out into another variable and then using that variable to grab the level 2 page content.
Let me know if this helps at all.
is working on a reply...