0

在我们的本地化数据库中,我们有以下对象:

public class Text
{
  public string Key { get; set; }
  public string LanguageId { get; set; }
  public string Value { get; set; }
}

我需要一个 Linq 语句将这些记录展平为一个结构,该结构具有 LanguageIds 作为属性,其值如下:

public class ...
{
  public string Key { get; set; }
  public string En { get; set; }
  public string Ge { get; set; }
  public string Fr { get; set; }
  ...
}

当然,这可以通过一些精心设计的“for each”来实现,但必须有一个巧妙的 Linq 语句一次性完成。那里有链接大师吗?

4

4 回答 4

4

我偶然发现了这一点,只是想为这个问题添加我的解决方案。我冒昧地围绕表达式生成了一个可运行的小示例:

public class LocalizationManager
{
    public IDictionary<string, Localization> LostInTranslation()
    {
        var input = new[]
                        {
                            new Text {Key = "ORDINAL_1", LanguageId = "En", Value = "first"},
                            new Text {Key = "ORDINAL_1", LanguageId = "Fr", Value = "premier"},
                            new Text {Key = "ORDINAL_1", LanguageId = "De", Value = "erste"},
                            new Text {Key = "ORDINAL_2", LanguageId = "En", Value = "second"},
                            new Text {Key = "ORDINAL_2", LanguageId = "Fr", Value = "deuxième"},
                            new Text {Key = "ORDINAL_2", LanguageId = "De", Value = "zweite"},
                            new Text {Key = "ORDINAL_3", LanguageId = "En", Value = "third"},
                            new Text {Key = "ORDINAL_3", LanguageId = "Fr", Value = "troisième"},
                            new Text {Key = "ORDINAL_3", LanguageId = "De", Value = "dritte"},
                        };

        var output =
            input.GroupBy(text => text.Key).Select(
                group =>
                group.Aggregate(new Localization { Key = group.Key },
                                (translation, text) =>
                                    {
                                        translation.GetType().GetProperty(text.LanguageId).SetValue(translation, text.Value, null);
                                        return translation;
                                    }));

        return output.ToDictionary(key => key.Key);
    }
}

public class Text
{
    public string Key { get; set; }
    public string LanguageId { get; set; }
    public string Value { get; set; }
}

public class Localization
{
    public string Key { get; set; }
    public string En { get; set; }
    public string De { get; set; }
    public string Fr { get; set; }
}

虽然这似乎是对 Aggregate() 的轻微歪曲,但我认为它的精神是真实的。请注意,所有语言都必须作为“本地化”对象的属性存在,否则您将收到 NullReferenceException,因为 GetProperty() 返回 null。

于 2012-11-14T10:52:28.810 回答
3

尝试以下操作:

var flattenTexts = texts
    .GroupBy(x => x.Key)
    .Select(x => new { Key = x.Key, 
                       De = x.First(y => y.LanguageId == "De").Value, 
                       En = x.First(y => y.LanguageId == "En").Value 
                     });

你当然需要知道你有哪些语言......

于 2012-09-03T08:28:59.367 回答
0

If I understood what you mean you can create the second Object out of the 1st one and integrate this in your Linq statement for example:

  var result = TextObjectList.Select(x => new LanguageObject(x))

And in the LanguageObject Constructor set the values up to the LanguageId.

于 2012-09-03T08:20:49.110 回答
0

您可以执行以下操作:

typeof(MyClass).GetProperties().AsEnumerable().Select(x => new () {Key = x.Name, En = "English", Ge = "German (shouldn't it be De?)" ... }
于 2012-09-03T08:21:09.613 回答