[英]Expression Property with Generic Parameter
Here goes my first question in Stack Overflow. 这是我在Stack Overflow中的第一个问题。 So, please bear with me and I hope I can point you right to my problem... (sorry if this is a repeat, but I searched a lot and I couldn't find an answer for the exact issue I'm having nor I could figure it out from other answers).
因此,请您耐心等待,我希望您能指出我的问题...(很抱歉,如果重复一遍,但是我进行了很多搜索,但我找不到确切问题的答案,也没有找到答案。我可以从其他答案中找出来)。
Let me show you the code first so you can get some context (spoiler: the issue I'm having is with keySelector
). 首先让我向您展示代码,以便获得一些上下文信息(扰流器:
keySelector
是我遇到的问题)。
I have this method in a class: 我在课堂上有这个方法:
public class BaseTableDataStore<T> : BaseTableDataStore, ITableDataStore<T> where T : Models.EntityData
{
...
public virtual async Task<IEnumerable<T>> GetItemsQueryAsync<Tkey>(Expression<Func<T, bool>> predicate, Expression<Func<T, Tkey>> keySelector)
{
...
return await Table.Where(predicate).OrderBy(keySelector).ToEnumerableAsync();
...
}
...
}
Then, I have this other class: 然后,我有另一堂课:
public class ConnectedObservableCollection<T> : ObservableRangeCollection<T> where T : EntityData
{
ITableDataStore<T> table;
public Expression<Func<T, bool>> Predicate { get; set; }
public Expression<Func<T, long?>> KeySelector { get; set; }
...
public async Task Refresh()
{
...
_items = await table.GetItemsQueryAsync(Predicate, KeySelector);
...
}
...
}
Finally, I have the following code in another class: 最后,我在另一个类中有以下代码:
ConnectedObservableCollection<MeterEntry> meterEntries;
...
meterEntries.Predicate = (s => s.Type == 1);
meterEntries.KeySelector = (s => s.Value);
await MeterEntries.Refresh(wait);
...
That will of course work fine when s.Value
is long?
当
s.Value
long?
时,那当然可以工作long?
. 。 But I would like to make this property generic so I can use any type of parameter instead of just
long?
但是我想使这个属性通用,所以我可以使用任何类型的参数,而不仅仅是
long?
参数long?
public Expression<Func<T, long?>> KeySelector { get; set; }
I hope it's clear enough... 我希望已经足够清楚了...
Thanks! 谢谢!
If you want to change this: 如果要更改此设置:
public Expression<Func<T, long?>> KeySelector { get; set; }
to this (replace long
with a second generic type) 为此(用第二个泛型类型替换
long
)
public Expression<Func<T, T2>> KeySelector { get; set; }
then that second generic type must be included in your class definition. 那么该第二泛型类型必须包含在您的类定义中。
Change 更改
public class ConnectedObservableCollection<T>
: ObservableRangeCollection<T> where T : EntityData
to 至
public class ConnectedObservableCollection<T, T2>
: ObservableRangeCollection<T> where T : EntityData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.