1

I want to write a test for my own GroupBy but i have a problem.

The GroupBy method:

GroupBy method.img

Part of my test code:

        Func<ProductsList.Product, string> elementSelector = x => x.Name;

        Func<ProductsList.Product, int> keySelector = x => x.Ingredients.Count;

        Func<int, IEnumerable<string>, KeyValuePair<int, IEnumerable<string>>> resultSelector = (IngredientsCount, ProductList) =>
        {

            return new KeyValuePair<int, IEnumerable<string>>(IngredientsCount, ProductList);
        };

        var comparer = new ProductListComparer();

        var result = LinqFunctions.GroupBy(products,
                                            x => keySelector(x),
                                            y => elementSelector(y),
                                            (int IngredientsCount, IEnumerable<string> ProductNames) => resultSelector(IngredientsCount, ProductNames),
                                            comparer
                                            );

       //rest of the code

    }

I get the following error:

error CS0411: The type arguments for method 'LinqFunctions.GroupBy<TSource, TKey, TElement, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, Func<TKey, IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I tried everything but can't find a solution. Any help please?

Ciprian
  • 23
  • 1
  • 5

1 Answers1

0

You can try specifying the type arguments explicitly. Like this:

var result = LinqFunctions.GroupBy<ProductsList.Product, int, string, KeyValuePair<int, IEnumerable<string>>>(...

UPD.

Also it seems the call should be like this one:

var result = LinqFunctions.GroupBy(products,
                                        keySelector,
                                        elementSelector,
                                        resultSelector,
                                        comparer
                                        );

so the source of error - providing wrong Func for resultSelector

Guru Stron
  • 102,774
  • 10
  • 95
  • 132