我用这个代码来计算某个高度最常见的出现次数。然而,我似乎不能这样做,因为下面的错误是显而易见的。有人能告诉我怎么解决这个问题吗?输入变量lines
的类型为IEnumerable<(Vector4D, Vector4D)>
。
Error CS0411 The type arguments for method
'Enumerable.GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)'
cannot be inferred from the usage. Try specifying the type arguments explicitly.
lines.Select(line => (Y: line.Item1.Y, Vector: line.Item1 - line.Item2))
.Where(pair => Math.Abs(Vector4D.DotProduct(new Vector4D(1, 0, 0, 0), pair.Vector)) > 0.9)
.GroupBy(pair => pair.Y, new CompareWithTolerance((float)verticalTolerance))
.Select(group => (Y: group.Key, Length: group.Sum(x => x.Vector.Length))
.OrderByDescending(pair => pair.Y * pair.Length));
internal class CompareWithTolerance : EqualityComparer<float>
{
private float _tolerance;
public CompareWithTolerance(float tolerance)
{
_tolerance = tolerance;
}
public override bool Equals(float a, float b)
{
return Math.Abs(a - b) < _tolerance;
}
public override int GetHashCode(float f)
{
return f.GetHashCode();
}
}
根据我收到的反馈,似乎我忘记将比较值的类型更改为float,这导致它抛出错误。
我在回复中提出的问题很容易解决,只需添加一些括号。