Home > other >  GroupBy cannot infer type arguments from usage
GroupBy cannot infer type arguments from usage

Time:06-16

I have this code to calculate the most common occurance of a certain height. However, I cannot seem to do it like this as is evident by the following error. Would anyone be able to tell me how I can resolve this? The inputted variable lines is of type 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();
        }
    }

CodePudding user response:

Based on the feedback I received, it seems I forgot to change the type of the comparing value to float, which was causing it to throw an error.

The resulting issue I presented in my response was easily fixed simply by placing some additional brackets.

  • Related