1

我使用表格“TLanguage”来记录我网站的标签结果。我在这张表中有 4 列:法语、英语、德语和西班牙语。

在 MVC 应用程序中,我使用以下查询:

 var req = (from TYP in context.TYP_TypeMission
                   join ML in context.TLanguage on TYP.IDTMultiLanguage equals ML.IDTMultiLanguage
                  where TYP.IDTFiliale == idFiliale
                  orderby TYP.LibTypeMission
                  select new SelectListItem
                  {
                      Selected = TYP.IdTypeMission == idTypeMission,
                      Value = SqlFunctions.StringConvert((double)TYP.IdTypeMission),
                      Text = ML.French
                  }).ToList();

如何根据我的网站语言在查询中通过 ML.English 或 ML.German 更改 ML.French?

是否可以在查询中创建间接?

4

2 回答 2

1

可以像这样对映射进行参数化:

public class TLanguageMap : EntityTypeConfiguration<TLanguage>
{
    public TLanguageMap(string language)
    {
        this.HasKey(t => t.TLanguageId);
        this.Property(t => t.Translation).HasColumnName(language);
    }
}

在上下文中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new TLanguageMap(this._language));
}

及其构造函数:

public LocalizableContext(string language)
{
    this._language = language;
}

现在,在构建上下文时,您可以确定它是哪种语言:

var context = new LocalizableContext("French");

查询将始终是:

...
select new SelectListItem
{
    Selected = TYP.IdTypeMission == idTypeMission,
    Value = SqlFunctions.StringConvert((double)TYP.IdTypeMission),
    Text = ML.Translation
})

enum您可能希望通过使用for 语言和switch语句来获取数据库列名来使其更加健壮。

于 2013-04-29T19:23:27.857 回答
0

您可以将查询的开头保存到变量中,稍后仅更改最后一部分:

var query = from TYP in context.TYP_TypeMission
            join ML in context.TLanguage on TYP.IDTMultiLanguage equals ML.IDTMultiLanguage
            where TYP.IDTFiliale == idFiliale
            orderby TYP.LibTypeMission
            select new { ML, TYP };

List<SelectListItem> req;

if(Site.Lang == "DE")
{
    req = (from item in query
           select new SelectListItem
           {
               Selected = item.TYP.IdTypeMission == idTypeMission,
               Value = SqlFunctions.StringConvert((double)item.TYP.IdTypeMission),
               Text = item.ML.German 
           }).ToList();
}
else if(Site.Lang == "FR")
{
    req = (from item in query
           select new SelectListItem
           {
               Selected = item.TYP.IdTypeMission == idTypeMission,
               Value = SqlFunctions.StringConvert((double)item.TYP.IdTypeMission),
               Text = item.ML.French
           }).ToList();
}
else
{
    req = (from item in query
           select new SelectListItem
           {
               Selected = item.TYP.IdTypeMission == idTypeMission,
               Value = SqlFunctions.StringConvert((double)item.TYP.IdTypeMission),
               Text = item.ML.English
           }).ToList();
}

ToList()在调用之前不会执行查询。

于 2013-04-29T13:03:15.433 回答