Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have a node with a uTaxonomy property. When accessing my property I get this: "System.Collections.Generic.List`1[ASP.Taxonomy]".
When looping the Generic.List I get all the names of the taxonomies:
foreach(var item in employee.departments){ @item }
How can I, in this foreach, check if the list contains for example "accounting"?
I have tried using employee.departments.Contains("accounting"), but I am getting this:
The best overloaded method match for 'System.Collections.Generic.List<ASP.Taxonomy>.Contains(ASP.Taxonomy)' has some invalid arguments
Hi Søren,
I think you have a problem, because when you are trying to call the Contains method, departments property is a dynamic type.
Try this:
@(((string)item.ToString()).Contains("accounting"))
I hope I understood you correctly.
I'll add another method that will return (true/false), if the required value is in the collection or not
Vitaly
Hi Vitaly
Thanks! I got it to work by doing this:
bool showEmployee = false; foreach(var item in employee.departments){ if((((string)item.ToString().ToLower()).Contains(kategori))){ showEmployee = true; } } if(showEmployee){ <div>Employee data</div> }
It's not pretty, but it solves my problem :-)
You can try to add below method in TaxonomyExtensions class in Taxonomy.cs which is located in the App_Code folder.
public static bool HasValue(this IEnumerable<Taxonomy> values, string value) { return values.Any(x => x.Item.Name == value); }
How to use:
@{ var taxonomy = Model.Content.GetPropertyValue<List<Taxonomy>>("yourPropertyName"); } @if(taxonomy.HasValue("accounting")){ // first code } else { // second code }
Best regards, Vitaly
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Check if a uTaxonomy property contains a string
I have a node with a uTaxonomy property. When accessing my property I get this: "System.Collections.Generic.List`1[ASP.Taxonomy]".
When looping the Generic.List I get all the names of the taxonomies:
How can I, in this foreach, check if the list contains for example "accounting"?
I have tried using employee.departments.Contains("accounting"), but I am getting this:
Hi Søren,
I think you have a problem, because when you are trying to call the Contains method, departments property is a dynamic type.
Try this:
I hope I understood you correctly.
I'll add another method that will return (true/false), if the required value is in the collection or not
Vitaly
Hi Vitaly
Thanks! I got it to work by doing this:
It's not pretty, but it solves my problem :-)
Hi Søren,
You can try to add below method in TaxonomyExtensions class in Taxonomy.cs which is located in the App_Code folder.
How to use:
Best regards, Vitaly
is working on a reply...