【发布时间】:2020-04-30 10:20:29
【问题描述】:
我想为我自己的 GroupBy 编写一个测试,但我遇到了问题。
GroupBy 方法:
我的部分测试代码:
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
}
我收到以下错误:
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.
我尝试了所有方法,但找不到解决方案。有什么帮助吗?
【问题讨论】:
-
能否提供
LinqFunctions.GroupBy的定义? -
我添加了 GroupBy
-
这是一个自制函数,因为
LinqFunctions不是 LINQ 命名空间。没有看到函数,这个问题无法回答。 -
@Ciprian 在我的回答中看到更新。