You have two distinctly different collections there - but they are both collections of IPublishedContent so you could merge the two together. There are lots of ways to do it - but to give you an idea:
var combined = new List<IPublishedContent>();
combined.AddRange(Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId));
combined.AddRange(Model.Content.Decendants("article"));
foreach(var item in combined.OrderBy(x => x.Name))
{
// do something ...
}
Just FYI - if you're using Merchello 2.x you should change the
Model.Content.GetPropertyValue<ProductContentListView>(propertyId) it's basically the same thing but has a bit more information and allows us to expose more targeted extension methods.
That kind of did the trick... - however, I used to get the Collections() property
this way:
foreach (var product in Model.Content.GetPropertyValue<IEnumerable<IProductContent>>("newestProducts"))
{
var collection = product.Collections().FirstOrDefault().ParentKey;
}
But with your example I get the message:
'IPublishedContent' does not contain a definition for 'Collections' and the best extension method overload 'ProductCollectionExtensions.Collections(IProuctContent)' requires a resiver of type 'IProductContent'
However, notice that this is only being used on the Product page itself and not while iterating through a collection as, depending on how you have things setup, create a lot of SQL calls if your not careful.
Also -
In your code
var collection = product.Collections().FirstOrDefault().ParentKey;
You might consider checking for null before trying to read the parent
I think I use Merchello ver. 2.1.0 - I'll upgrade to the latest version as soon as possible...
As I explained - I cannot get collections this way while using the new setup:
var collection = product.Collections().FirstOrDefault().ParentKey;
However - what I used it for was to get the Products root collection name (grand parent) like this: (for use in css)
var category = MerchelloContext.Current.Services.EntityCollectionService;
var collection = product.Collections().FirstOrDefault().ParentKey;
var categoryClass = category.GetByKey(new Guid(collection.ToString())).Name.ToLower();
Root Collection <--- this name
-Sub Collection
-Product <--- this product
Maybe your'e right - it might prodouce to many sql calls... - is there an easyer way to get the root collection name...?
Still a ways out on releasing - but it's based largely on the discussion above - just has a bit more optimization. Let me know if you have problems in 2.2.0 ...
Display Merchello Proucts with Umbraco nodes
As head line says - Can I combine these two in one display...?
Lests say I has these:
Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId)
Model.Content.Descendants("article")
How can I combine theese in one foreach loop...? (they both has the same properties)
Hi Robin,
You have two distinctly different collections there - but they are both collections of
IPublishedContent
so you could merge the two together. There are lots of ways to do it - but to give you an idea:Just FYI - if you're using Merchello 2.x you should change the
Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId)
to
Model.Content.GetPropertyValue<ProductContentListView>(propertyId)
it's basically the same thing but has a bit more information and allows us to expose more targeted extension methods.That kind of did the trick... - however, I used to get the Collections() property this way:
But with your example I get the message:
I really ned the value of Collections() :-|
Hey Robin,
What Merchello version are you working with?
In the FastTrack starter (I think) we do something similar to what you are trying to do... https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.FastTrack.Ui/Views/ftProduct.cshtml#L82
However, notice that this is only being used on the Product page itself and not while iterating through a collection as, depending on how you have things setup, create a lot of SQL calls if your not careful.
Also -
In your code
You might consider checking for
null
before trying to read the parentI think I use Merchello ver. 2.1.0 - I'll upgrade to the latest version as soon as possible...
As I explained - I cannot get collections this way while using the new setup:
var collection = product.Collections().FirstOrDefault().ParentKey;
However - what I used it for was to get the Products root collection name (grand parent) like this: (for use in css)
Maybe your'e right - it might prodouce to many sql calls... - is there an easyer way to get the root collection name...?
Not in version 2.2.0 - In the 2.3.0 release we've added a bunch of functionality to the IProductCollection - https://github.com/Merchello/Merchello/pull/1793
Still a ways out on releasing - but it's based largely on the discussion above - just has a bit more optimization. Let me know if you have problems in 2.2.0 ...
is working on a reply...