我有一个SQL Server数据库,其中使用了一种自定义方法来存储不同语言的特定列的翻译。它的工作原理是";主";实体的表,只包含实体的实际数据,但不包含实体名称的列。相反,还有一个额外的表,只用于实体名称的翻译。该表由外键(实体Id(、用于存储语言说明符的列(例如"en"或"fr"(和用于翻译名称的列组成。这里有一个例子:
-
实体
Product
的表格(非常简化(:id 价格 1 4 2 1 我只是检查了您的场景,并使用了Translation List而不是Dictionary作为Name。工作小提琴
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List<TblProductTranslation> lst=new List<TblProductTranslation>(); lst.Add(new TblProductTranslation{ProductId=1,Language="de",ProductName="Brot"}); lst.Add(new TblProductTranslation{ProductId=1,Language="en",ProductName="Bread"}); lst.Add(new TblProductTranslation{ProductId=2,Language="de",ProductName="Brot"}); lst.Add(new TblProductTranslation{ProductId=2,Language="en",ProductName="Apple"}); List<TblProduct> products=new List<TblProduct>(); products.Add(new TblProduct {Id=1,Price=4}); products.Add(new TblProduct {Id=2,Price=1}); var result = (from c in products select new Product { ProductId=c.Id, Price=c.Price, Name=lst.Where(x=>x.ProductId==c.Id).Select(y=>new Translation(){Language= y.Language,ProductName=y.ProductName}).ToList() }).ToList(); foreach(var item in result) { foreach(var item1 in item.Name) { Console.WriteLine("Id: {0} , Price: {1}, Name: {2}",item.ProductId,item.Price,item1.Language+" - "+item1.ProductName); } } } public class TblProductTranslation { public int ProductId { get; set; } public int Price { get; set; } public string Language { get; set; } public string ProductName { get; set; } } public class TblProduct { public int Id { get; set; } public int Price { get; set; } } public class Product { public int ProductId { get; set; } public List<Translation> Name { get; set;} public int Price { get; set; } } public class Translation { public string Language { get; set; } public string ProductName { get; set; } } }