我必须在生产应用程序中支持多种语言。
很多实体框架查询都是从数据库中获取数据,像这样的延迟IQueryable列表:
public IQueryable<Request> GetDeferredRequests()
{
return _dbContext.Set<Request>();
}
POCO类如下:
public partial class Request
{
public int RequestID { get; set; }
public string StatusName { get; set; }
public string RequestType { get; set; }
}
数据传输对象看起来像这样:
public class RequestDTO
{
public int RequestID { get; set; }
public string StatusName { get; set; }
public string RequestType { get; set; }
}
[之后,我将EF POCO实体映射到数据传输对象。为了支持多种语言,我想通过映射中的数据库值来获取资源值,如下所示:
public IQueryable<RequestDTO> MapRequests(IQueryable<Request> requests)
{
Dictionary<string, string> resoures = new Dictionary<string, string>();
System.Resources.ResourceSet resources = DatabaseResources.ResourceManager.GetResourceSet(new System.Globalization.CultureInfo("en"), true, true);
foreach (DictionaryEntry resource in resources)
{
resoures.Add(resource.Key.ToString(), resource.Value.ToString());
}
return requests.Select(c => new RequestDTO()
{
RequestID = c.RequestID,
StatusName = resoures.Single(r => r.Key == c.StatusName).Value,
RequestType = resoures.Single(r => r.Key == c.RequestType).Value
});
}
问题是最后一个命令引发以下异常:
LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method, and this method cannot be translated into a store expression.
不幸的是,通过ToList()将IQueryable转换为IEnumerable并不是一种选择,因为我不想将列表移动到内存中。