繁体   English   中英

GroupBy 无法从用法推断类型参数

[英]GroupBy cannot infer type arguments from usage

我有这段代码来计算某个高度的最常见出现。 但是,我似乎不能这样做,因为以下错误很明显。 谁能告诉我如何解决这个问题? 输入的变量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();
        }
    }

根据我收到的反馈,我似乎忘记将比较值的类型更改为浮点数,这导致它抛出错误。

我在回复中提出的由此产生的问题很容易通过放置一些额外的括号来解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM