这是我的Buinsess模型,非常缩小。
public class Container
{
public List<Property> Properties { get; set; }
}
public class Property
{
public string Value { get; set; }
}
有一个容器列表
任何想法?
至1。 我用过这个
var someAmountOfProperties = containers.All(x => x.Properties.Count() == containers.First().Properties.Count);
但我真的不喜欢它。因为引用了第一个对象。
VALID ,因为金额相同且值相等。
var containers = new List<Container>
{
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "2"},
}
},
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "2"},
}
},
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "2"},
}
},
};
VALID ,因为金额相同且值相等(第1和第2个相同)。
var containers = new List<Container>
{
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "5"},
}
},
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "5"},
}
},
new Container
{
Properties = new List<Property>
{
new Property {Value = "2"},
new Property {Value = "5"},
}
},
};
答案 0 :(得分:2)
您可以使用Distinct
。第一:
containers.Select(x => x.Properties.Count()).Distinct().Count() == 1
第二
container.Properties.Select(x => x.Value).Distinct().Count() == 1
您也可以同时检查所有容器的第二个条件:
containers.All(c => c.Properties.Select(x => x.Value).Distinct().Count() == 1)
如果您的案件中的0是有效计数,则可以将== 1
更改为<= 1
。
编辑。对于第二个,这是迁移工作的东西:
containers.SelectMany(c => c.Properties.Select((x, i) => new {Value = x.Value, Index = i}))
.GroupBy(x => x.Index)
.All(g => g.Select(x => x.Value).Distinct().Count() == 1)