简体   繁体   English

为IQueryable创建动态类型 <T> 在#中运行时

[英]Creating Dynamic types to IQueryable<T> at runtime in #

I'm developing a multilingual news portal website using asp.net and c#. 我正在使用asp.net和c#开发一个多语言新闻门户网站。 Currently the site supports two languages and it will be extended to other additional languages when required. 目前,该网站支持两种语言,并将在需要时扩展为其他其他语言。 So it is my job to create the basic framework for the site. 因此,为网站创建基本框架是我的工作。 Based on the client requirements,the content to be stored in the database for the website for the different languages may be different. 根据客户的要求,针对不同语言的网站要存储在数据库中的内容可能会有所不同。 So, I have created different tables to store the News in different languages (like tblNews_EN, tblNews_am, etc) to store content for Enlish or Amharic languages. 因此,我创建了不同的表以不同的语言(例如tblNews_EN,tblNews_am等)存储新闻,以存储Enlish或Amharic语言的内容。

Now I have classes to retrieve the latest news but I want to generalize those classes and retrieve the latest news for the specific culture being requested by the user. 现在,我具有检索最新新闻的类,但是我想归纳这些类并检索用户所请求的特定区域性的最新新闻。 Initially I have something like the following which only work to retrieve latest news from the tblNews_EN table. 最初,我有类似以下内容的内容,这些内容只能从tblNews_EN表中检索最新新闻。

public IQueryable<GetNews> GetTopExtraNews()
 {

        var topExtraNews = from p in db.GetNews
                           where p.TopOrderPriority == 2
                           orderby p.NewsDate descending
                           select p;
        return topExtraNews;
}

Here GetNews is my Entity class that corresponds to the tblNews_EN table. 这里的GetNews是我的Entity类,它对应于tblNews_EN表。

Now I want to generalize this one so that it will return objects from different tables depending on the culture selected. 现在,我想对此进行概括,以便根据所选区域性从不同的表返回对象。 I tried to use something like IQueryable but I dont think this is the best option to deal with my case. 我尝试使用类似IQueryable的方法,但我认为这不是处理我的情况的最佳选择。 So, is there any better option to do this? 那么,有没有更好的选择呢?

If I understood well all of your tables have the same structure. 如果我很了解,您的所有表都具有相同的结构。

Based on that just send the table as a parameter to your method and use this parameter to your query: 基于此,只需将表作为参数发送给您的方法,然后在查询中使用此参数:

public IQueryable<GetNews> GetTopExtraNews(typeofYourTable myTable)
{

        var topExtraNews = from p in myTable
                           where p.TopOrderPriority == 2
                           orderby p.NewsDate descending
                           select p;
        return topExtraNews;
}

Of course you have to set the myTable parameter to the correct language table before calling your method. 当然,在调用方法之前,必须将myTable参数设置为正确的语言表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM