I’ve been wrestling with this problem for a few hours now and I’m beginning to think I’m too far down the rabbit hole. Before I give up entirely, I’m hoping someone might be able to shed some light on this for me…
public class Object
{
public Object()
{
Properties = new List<Property>();
}
public int Id { get; set; }
public IList<Property> Properties { get; set; }
}
public class Property
{
public string Name { get; set; }
public dynamic Value { get; set; }
}
[Test]
public void ShouldBeAbleToGroupAnArrayOfObjectsByASpecificProperty()
{
// Arrange
var objects = new List<Object>();
var doorObject = new Object() {
Properties = new List<Property>() {new Property() {Name = "Level", Value = "Level 1"}}
};
var wallobject = new Object() {
Properties = new List<Property>() { new Property() { Name = "Level", Value = "Level 2" } }
};
var chairObject = new Object() {
Properties = new List<Property>() { new Property() { Name = "Level", Value = "Level 2" } }
};
objects.Add(doorObject);
objects.Add(wallobject);
objects.Add(chairObject);
// Act
var groupBy = objects.SelectMany(obj => obj.Properties).GroupBy(props => props.Value);
// Assert
Assert.That(groupBy.Count(), Is.EqualTo(2));
}
This test passes, but the problem is that I’m returning an array of Properties. I’m attempting to get an array of Objects. Is what I’m trying to do possible?
How about this:
or