Hi - I have a document type with a property of RelatedLinks.
I would like to get the first link entered - if its there - using a Razor macro. I tried to use the Linq-like synax like: FirstOrDefault og ElementAt(0). But apparently they do not work with the list of linked lists.
I have managed to "hack" my way through but I would like to know if there is a more elegant way of achieving the same effect as FirstOrDefault og ElementAt(0).
Here is my hack:
@inherits umbraco.MacroEngines.DynamicNodeContext
@foreach (var item in @Model.NodeById(1520).Children.Where("archived != true").OrderBy("CreatedDate desc")) {
string href = "#"; string target = "";
foreach (var link in @item.NewsUrl) { target = link.newwindow == "1" ? " target=\"_blank\"" : ""; if (link.type=="internal") { href=umbraco.library.NiceUrl(int.Parse(link.link)); } else { href=link.link; } break; }
Unfortunately, the Count() method is not implemtented in 4.7. If you grab just the umbraco.MacroEngines.dll from one of the 4.7.1 nightlies, you can do it as simple as this:
I tried to use the MacroEngines.dll from the 4.7.1.408 build. However, that introduced a problem with it not being able to understand "ToString" on a date type:
Error loading Razor Script FrontpageNews.cshtml The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
How to get first link from RelatedLinks datatype
Hi - I have a document type with a property of RelatedLinks.
I would like to get the first link entered - if its there - using a Razor macro. I tried to use the Linq-like synax like: FirstOrDefault og ElementAt(0). But apparently they do not work with the list of linked lists.
I have managed to "hack" my way through but I would like to know if there is a more elegant way of achieving the same effect as FirstOrDefault og ElementAt(0).
Here is my hack:
@inherits umbraco.MacroEngines.DynamicNodeContext
@foreach (var item in @Model.NodeById(1520).Children.Where("archived != true").OrderBy("CreatedDate desc")) {
string href = "#";
string target = "";
foreach (var link in @item.NewsUrl) {
target = link.newwindow == "1" ? " target=\"_blank\"" : "";
if (link.type=="internal") {
href=umbraco.library.NiceUrl(int.Parse(link.link));
}
else {
href=link.link;
}
break;
}
<a href="@href">
<div class="newsBoxHeader">
<i>@item.CreatedDate.ToString("MMMM d, yyyy")</i>
<b>@item.Title</b>
</div>
<div class="newsBoxItem">
@item.Description
</div>
</a>
<div class="block10">
<img src="/images/site/divider.png" alt="divider" />
</div>
}
The thing that annoys me is the second inner foreach loop, to get the first item in the RelatedLinks field (NewsUrl).
Regards
Thomas
Forgot to mention that I am running Umbraco 4.7 and .NET4.
Unfortunately, the Count() method is not implemtented in 4.7. If you grab just the umbraco.MacroEngines.dll from one of the 4.7.1 nightlies, you can do it as simple as this:
Hi Sebastian - thanks for the reply. :)
I tried to use the MacroEngines.dll from the 4.7.1.408 build. However, that introduced a problem with it not being able to understand "ToString" on a date type:
Error loading Razor Script FrontpageNews.cshtml
The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
Any suggestions?
Thomas
Ehm, not really, I'd need to see some of your code to know what's going on.
Hi Sebastiaan - sorry for not answering before now.
I have decided to stick with the original solution and accept that it may not be so pretty. It works. :)
I will keep your suggestion in mind.
Thank you for taking the time to answer my post.
Kind regards
Thomas
is working on a reply...