我定义了一个网格字典:
Dictionary<Tuple<int, int>, decimal> _details;
其中键是Item1 -> ColumnIndex,Item2 -> RowIndex
我有一个复杂的分组方法,应该将 3 个字典项组合在一起,
从同一行和列是 1,2,3 或 4,5,6 或 7,8,9 或 10,11,12 然后处理它们做吗?
我的代码:
private bool IsValid() { // 检查每个值的数量和值或两者 null // 检查总数不为空
if (_details.Count() <= 0)
return false;
var rows = _details.GroupBy(c => c.Key.Item2);
foreach (var item in rows)
{
foreach (var subItem in item)
{
switch (subItem.Key.Item1)
{
case 1:
{
if (!item.Any(c => c.Key.Item1.In(2, 3)))
return false;
break;
}
case 2:
{
if (!item.Any(c => c.Key.Item1 == 1) || item.Any(c => c.Key.Item1 == 3))
return false;
break;
}
case 3:
{
if (!item.Any(c => c.Key.Item1 == 1) || item.Any(c => c.Key.Item1 == 2))
return false;
break;
}
// code continues the same way up to 12
return true;
}
编辑:数据
_details = new Dictionary<Tuple<int, int>, decimal>();
_details.Add(new Tuple<int, int>(1, 1), 10);
_details.Add(new Tuple<int, int>(2, 1), 10);
// if abouve data only added return true
_details.Add(new Tuple<int, int>(4, 2), 10);
_details.Add(new Tuple<int, int>(6, 2), 10);
// still true
_details.Add(new Tuple<int, int>(10, 4), 10);
_details.Add(new Tuple<int, int>(11, 4), 10);
//still true
_details.Add(new Tuple<int, int>(2, 2), 10);
_details.Add(new Tuple<int, int>(8, 1), 10);
_details.Add(new Tuple<int, int>(10, 3), 10);
// adding the last 3 return false