C# 使用反射和 linq 将通用对象属性替换为另一个值
我不太明白这一点。为了简单起见,我将解释我想要做什么。
目前,我正在开发一个使用存储库模式的 nHibernate 项目。数据库内部有一堆具有“描述”字段的表。现在,我尝试本地化这些表以使用通用的“资源”表。
所以,我有这 2 个表:
表 1 - 资源: ResourceId、LanguageId、值
表 2 - LinkLabels: LinkLabelId、Description、ResourceId
然后有一个会话令牌,其中包含“LanguageId”,这是用户的首选语言。
好的,话虽如此,我需要做的是从 ResourcesTable 中获取“值”,其中 LinkLabels 中的 ResourceId = 来自具有“ResourceId”对象的资源的 ResourceId。但我必须用通用类型来做到这一点。
这是我到目前为止所拥有的:
public IQueryable<T> ToQueryable<T>()
{
try
{
PropertyInfo resourceProperty = typeof(T).GetProperty("ResourceKey");
if (resourceProperty != null)
{
// Somthing like this, but of course this won't work
// because x.ResourceId doesn't exist because of generic, and the select
// statement is invalue.
//
// return from x in this.session.Query<T>()
// join p in this.session.Query<Resource>() on x.ResourceId equals p.ResourceId
// where p.LanguageId = 1 // this will be from a session object, hardcoded to 1 for now
// select x where x.Description is p.value;
}
else
{
return this.session.Query<T>();
}
}
catch (Exception ex)
{
throw ex;
}
}
I can't quite figure this one out. To make it simple I'll explain what I'm trying to do.
Currently, I am working on a nHibernate project which uses a repository pattern. Inside the database are a bunch of tables that have "Description" fields. Now, I am trying to localize these tables to use a common "Resource" table.
So, I have these 2 tables:
Table 1 - Resources :
ResourceId, LanguageId, Value
Table 2 - LinkLabels :
LinkLabelId, Description, ResourceId
Then there is a session token that contains a "LanguageId" which is the preferred language of the user.
Okay, with that said, what I need to do is get the "Value" from the ResourcesTable where ResourceId from LinkLabels = ResourceId from Resources for objects that have a "ResourceId". But I've got to do this with a Generic type.
Here is what I have so far:
public IQueryable<T> ToQueryable<T>()
{
try
{
PropertyInfo resourceProperty = typeof(T).GetProperty("ResourceKey");
if (resourceProperty != null)
{
// Somthing like this, but of course this won't work
// because x.ResourceId doesn't exist because of generic, and the select
// statement is invalue.
//
// return from x in this.session.Query<T>()
// join p in this.session.Query<Resource>() on x.ResourceId equals p.ResourceId
// where p.LanguageId = 1 // this will be from a session object, hardcoded to 1 for now
// select x where x.Description is p.value;
}
else
{
return this.session.Query<T>();
}
}
catch (Exception ex)
{
throw ex;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个像这样的接口:
interface IResource {
int ResourceId { 获取;放;}
然后
使用约束:
public IQueryable ToQueryable() where T: IResource
{
...
然后
你必须让所有类型也实现 IResource。
Create an interface like this:
interface IResource {
int ResourceId { get; set;}
}
Then use a constraint:
public IQueryable ToQueryable() where T: IResource
{
...
}
Then you have to make all your types implement IResource as well.