如何像变量一样将表达式传递给方法?
我有 3 个几乎完全相同的方法:
protected DetachedCriteria GetAvailableFundIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Id));
}
protected DetachedCriteria GetAvailableCompanyIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Company.Id));
}
protected DetachedCriteria GetAvailableCategoryIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Category.Id));
}
其中 GetAvailableIdsPerDataUniverse()
定义为:
protected DetachedCriteria GetAvailableIdsPerDataUniverse()
{
return DetachedCriteria.For<Fund>()
.SetFetchMode<Fund>(f => f.ShareClasses, FetchMode.Join)
.CreateCriteria<Fund>(f => f.ShareClasses)
.Add(LambdaSubquery.Property<ShareClass>(x => x.Id).In(GetAvailableShareClassIdsPerDataUniverse()))
.Add<ShareClass>(f => f.ExcludeFromFT == false);
}
对于每个方法,唯一的区别是用于选择数据的表达式,即
f =>; f.Id
,f => f.Company.Id
和 f => f.Category.Id
有人可以告诉我是否可以将这些差异作为变量传递到 GetAvailableIdsPerDataUniverse()
中,这样我就只能有 1 个方法而不是 4 个方法?
I have 3 methods that are almost exactly identical:
protected DetachedCriteria GetAvailableFundIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Id));
}
protected DetachedCriteria GetAvailableCompanyIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Company.Id));
}
protected DetachedCriteria GetAvailableCategoryIdsPerDataUniverse()
{
return GetAvailableIdsPerDataUniverse()
.SetProjection(LambdaProjection.Property<Fund>(f => f.Category.Id));
}
where GetAvailableIdsPerDataUniverse()
is defined as:
protected DetachedCriteria GetAvailableIdsPerDataUniverse()
{
return DetachedCriteria.For<Fund>()
.SetFetchMode<Fund>(f => f.ShareClasses, FetchMode.Join)
.CreateCriteria<Fund>(f => f.ShareClasses)
.Add(LambdaSubquery.Property<ShareClass>(x => x.Id).In(GetAvailableShareClassIdsPerDataUniverse()))
.Add<ShareClass>(f => f.ExcludeFromFT == false);
}
For each method, the only difference is the expression that's used to select the data, i.e.
f => f.Id
, f => f.Company.Id
and f => f.Category.Id
Could somebody please tell me if it's possible to pass that those differences into GetAvailableIdsPerDataUniverse()
as a variable so I can have only 1 method instead of having 4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接在浏览器中输入,因此可能不会包含错误
编辑
我检查了 NHibernate.LambdaExtensions 的文档,LambdaProjection.Property 的类型是
所以参数类型应该是
typing directly in browser, so may not contain errors
Edit
I've checked documentation of NHibernate.LambdaExtensions, type of LambdaProjection.Property is
So parameter type should be