如何调用词典< K,V> .TryGetValue()其中K:谓词< T&GT,V:枚举 [英] How to call Dictionary<K, V>.TryGetValue() where K : Predicate<T>, V : enum

查看:181
本文介绍了如何调用词典< K,V> .TryGetValue()其中K:谓词< T&GT,V:枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

词典<谓语<双>中SomeEnum>

var dic = new Dictionary<Predicate<double>, SomeEnum>
{
    { (d) => d < 10, SomeEnum.Foo },
    { (d) => d > 90, SomeEnum.Bar }
};



我要打电话 TryGetValue(K,走出V)反对这样的:

dic.TryGetValue(99)

和接受

SomeStruct.Bar

TryGetValue第一个参数()谓词< T> ,而不仅仅是 T 。 ?如何做我想做的。

But first param for TryGetValue() is Predicate<T>, not just T. How can I do what I want?

我发现只有一个肮脏的解决方法:

I found only a dirty workaround:

var kpv = dic.FirstOrDefault(p => p.Key(99));
if (kpv.Key != null)
    var result = kpv.Value;



还有没有其他的办法呢?

Are there other ways?

< ?strong>或如何正确实现我的想法 - 声明的关键不是一个常数,而是像一个段

Or how to implement my idea properly? - declare a key not as a constant but like a segment.

推荐答案

有几件事情错在这里:

谓词<双> 不是合适的类型使用作为 TKEY的。对字典的关键是应该的确定的一个值,而不是计算值。

Predicate<double> is not an appropriate type to use as a TKey. The key for a dictionary is supposed to identify a value, not calculate a value.

这不会让使用lambda表达式或者任何意义。因为它们是匿名的,你就不会得到任何等价,并且不能使用的字典。双>

This wouldn't make any sense using lambdas either. Because they are anonymous, you wouldn't get any equivalence, and won't be able use a dictionary.

See this code sample for an illustration:

有关的说明,请参阅此代码示例; fn_1 = D => ð== 34.0d;
谓<双> fn_2 = D => ð== 34.0d;

//注:有不等于
如果(fn_1 == fn_2)
Console.WriteLine(这些都是平等?);

Predicate<double> fn_1 = d => d == 34.0d; Predicate<double> fn_2 = d => d == 34.0d; // Note: There are not equal if (fn_1 == fn_2) Console.WriteLine("These are Equal?");

如果任何东西,你可以使用委托列表并执行每一个找到匹配的那些,但在这一点上,你必须准备多个结果。如果你只希望得到一个结果,那么你必须考虑它的为了的谓词存储在您的列表中。

If anything, you could use a list of delegates and execute each one to find the ones that match, but at that point you must expect multiple results. If you only want to get a single result, then you have to consider which order the predicates are stored within your list.

不要滥用 KeyValuePair 作为不具有破解元组LT; T1,T2> 。这将是相当容易的创建一个既有谓词和SomeStruct的类。你看:

Don't misuse KeyValuePair as a hack for not having Tuple<T1,T2>. It would be fairly easy to create a class that has both a Predicate and a SomeStruct. Look:

public class MySegment
{   
     public Predicate<double> Predicate {get;set;}
     public SomeStruct Result {get;set;}
}

要经过谓词的序列,找到匹配的人是这样的:

To go through a sequence of predicates, and find the matching ones would look like this:

...
List<MySegment> list = new List<MySegment>();
...
list.Add(new MySegment { Predicate = d => d < 10, Result = SomeStruct.Foo });
list.Add(new MySegment { Predicate = d => d > 90, Result = SomeStruct.Bar });

...

public IEnumerable<SomeStruct> GetResults(double input)
{ 
    foreach (var item in list)
        if (item.Predicate(input))
             yield return item.Result;
}

这篇关于如何调用词典&LT; K,V&GT; .TryGetValue()其中K:谓词&LT; T&GT,V:枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆