C# Lambda“无法从用法推断”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:53:16  来源:igfitidea点击:

Lambda "cannot be inferred from the usage"

c#linqlambda

提问by qui

I have the following dictionary declared:

我声明了以下字典:

private readonly Dictionary<int, Image> dictionary;

And I have a method, which is causing a compiler error:

我有一个方法,它导致编译器错误:

    public IQueryable<Image> Find(Func<Image, bool> exp)
    {
        return  dictionary.Single(exp);
    }

The error i get is:

我得到的错误是:

Error   1   The type arguments for method 'System.Linq.Enumerable.Single<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. C:\work\MSD-AIDS-Images\MSD-AIDS-Images-Test\TestImageRepository.cs 34  30  MSD-AIDS-Images-Test

I have tried googling around, I cant seem to find anything definitive as to what I am doing wrong

我试过谷歌搜索,我似乎无法找到任何关于我做错了什么的确切信息

Edit - This is monday morning at work.

编辑 - 这是周一早上上班。

I meant to put "where", not single

我的意思是把“哪里”,而不是单身

Edit 2!

编辑2!

Ok, the code is now this:

好的,现在的代码是这样的:

public IQueryable<Image> Find(Func<Image, bool> exp)
{
    return dictionary.Values.Where(exp);
}

Now I get the following error:

现在我收到以下错误:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<MSD_AIDS_Images_Data.Image>' to 'System.Linq.IQueryable<MSD_AIDS_Images_Data.Image>'. An explicit conversion exists (are you missing a cast?)    C:\work\MSD-AIDS-Images\MSD-AIDS-Images-Test\TestImageRepository.cs 34  20  MSD-AIDS-Images-Test

As an FYI, the method there is for implementing an interface, the declaration for that is:

作为仅供参考,用于实现接口的方法,其声明是:

IQueryable<T> Find(Func<T, bool> exp);

This has got more complicated than it should be!

这变得比它应该的更复杂!

采纳答案by Marc Gravell

What is it you are trying to do? For example, Singlewill return an instance of T, not an IQueryable<T>(and for objects, you should probably be using IEnumerable<T>anyway)...

你想做什么?例如,Single将返回 的实例T,而不是 an IQueryable<T>(对于对象,您可能IEnumerable<T>无论如何都应该使用)...

It feels like you want:

感觉就像你想要:

public Image Find(Func<Image, bool> predicate)
{
    return dictionary.Values.Single(predicate);
}

Of course, you could do this globally as an extension method via something like:

当然,您可以通过以下方式作为扩展方法在全局范围内执行此操作:

(editincludes Wherefrom question edit)

编辑包括Where来自问题编辑)

static class DictionaryExtensions
{
    public static TValue FindSingle<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary,
        Func<TValue, bool> predicate)
    {
        return dictionary.Values.Single(predicate);
    }
    public static IEnumerable<TValue> Find<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary,
        Func<TValue, bool> predicate)
    {
        return dictionary.Values.Where(predicate);
    }
}

(I'm not sure it is much harder for the caller to do this themselves, though)

(不过,我不确定调用者自己做这件事要困难得多)

回答by Anton Gogolev

IDictionary<TKey, TValue>implements IEnumerable<KeyValuePair<TKey,TValue>>, so your Funcshould look more like Func<KeyValuePair<TKey,TValue>>.

IDictionary<TKey, TValue>实现IEnumerable<KeyValuePair<TKey,TValue>>,所以你Func应该看起来更像Func<KeyValuePair<TKey,TValue>>.

回答by Amy B

An explicit conversion exists (are you missing a cast?)

存在显式转换(您是否缺少演员表?)

There are two Wheremethods, one on System.Linq.Enumerable(which it is using), and one on System.Linq.Queryable(which you think it should be using, according to that return type).

有两种Where方法,一种用于System.Linq.Enumerable(它正在使用),另一种用于System.Linq.Queryable(根据该返回类型,您认为它应该使用)。

You need to do one of the following:

您需要执行以下操作之一:

  • change the return type to IEnumerable<Image>- I recommend this!
  • change the method call to Queryable.Where(dictionary.Values.AsQueryable(), exp)
  • call AsQueryable()
  • 将返回类型更改为IEnumerable<Image>- 我推荐这个!
  • 将方法调用更改为 Queryable.Where(dictionary.Values.AsQueryable(), exp)
  • 称呼 AsQueryable()