通用方法和谓词的问题 [英] Problems with generic methods and predicates
问题描述
我目前有这种通用方法:
I currently have this generic method:
public virtual T Get(Expression<Func<T, bool>> predicate)
{
return this.Query.Where(predicate).FirstOrDefault();
}
以下是我如何调用该方法的示例:
Here's an example of how I call the method:
ST = testTable.Get( u => u.PartitionKey == pKey & u.RowKey == rKey );
我对泛型真的不太了解,我需要做的是拥有一个将partitionKey和rowKey作为参数并执行Get的方法.像这样:
I really don't understand much about generics and what I need to do is to have one method that takes the partitionKey and rowKey as arguments and performs a Get. Something like this:
ST = testTable.Get(pkey,rkey);
外面有人知道我该怎么做吗?
Is there anyone out there who knows how I could do this?
推荐答案
您需要制作一个具有这些参数的函数,并使用使用该参数的lambda表达式调用第一个函数.
You need to make a function that takes those parameters and calls the first function with a lambda expression that uses the parameters.
public T Get(pkey,rkey)
{
return Get(u => u.PartitionKey == pKey & u.RowKey == rKey);
}
如果不限制T
继承具有这些属性的类型,则需要创建一个扩展方法,该扩展方法使用具有那些属性的类型参数来扩展您的类.
If T
isn't constrained to inherit a type with those properties, you would need to create an extension method that extends your class with a type parameter that has those properties.
这篇关于通用方法和谓词的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!