C# 使用反射和 linq 将通用对象属性替换为另一个值

发布于 2024-10-21 12:09:09 字数 1352 浏览 2 评论 0原文

我不太明白这一点。为了简单起见,我将解释我想要做什么。

目前,我正在开发一个使用存储库模式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-10-28 12:09:09

创建一个像这样的接口:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文