I need to select all Places with a particular tag then iterate over them:
public class Place {
public int Id { get; set; }
public string PlaceName { get; set; }
public IEnumerable<Tag> Tags { get; set; }
}
public class Tag {
public int Id { get; set; }
public string TagName { get; set; }
}
Each place can have multiple tags. Each tag can belong to multiple places.
In Linq - I need to select all Places that have a particular tag - eg this tag:
var Bar = new Tag {
Id = 1,
TagName = "Bar"
}
I've tried this:
foreach (var Bar in Model.Places.Where(x => x.Tags.Contains(Bar)))
But the foreach never gets iterated over - the Contains returns nothing (and many of the Places do have a Bar object in their collection of Tags)
I found this - it appears to say my approach should work?
Linq to Select Parent Objects Where Child Objects Have a Matching Child Object
Should this work / am I missing something? Is there a better way to do it?
Thanks.