Because of new requirements, we must add same translations to the current development, built in Entity-Framework 4, and it uses SQL Server. So, if we have an alert, for example, we need to bring the data by checking on translation some values, such as its title or description.
Table:
Alert
- AlertId int
- AlertTypeId int
- AlertTextEnum int
Translation
- FieldId int
- FieldName string
- Code string
- Description string
- LanguageId int
So, if we want to get the text for AlertType, we have to join both tables and make a relation with Translation.FieldName == "AlertTypeEnum", and Translation.Code == Alert.AlertTypeEnum. Mainly, this is the way.
Here my dirty approach checked on LinqPad but does not work with this version of Entity-Framework, because I cannot use the ToString method.
var o = Translations.Where(m => m.LanguageId == 1 && new List<string> {
"AlertTypeId"
}.Contains(m.FieldName));
var list = from item in Alerts
itemTranslation in o item.AlertTypeId.ToString() equals itemTranslation.Code
into ps
from itemTranslation in ps.DefaultIfEmpty()
select new
{
AlertId = item.AlertId ,
AlertTypeDescr = itemTranslation.Description
}.ToList();
Because this doesn't work, I'd tried to bring before the first query and parse Code into an integer, but it doesn't work either.
var o = Translations.Where(m => m.LanguageId == 1 && new List<string> {
"AlertTypeId"
}.Contains(m.FieldName)).
Select (x => new {
Code = (int)( x.Code),
FieldName = x.FieldName,
Description = x.Description
}).ToList();
var list = from item in Alerts
join itemTranslation in o
on item.AlertTypeId equals itemTranslation.Code
select new
{
AlertId = item.AlertId ,
AlertTypeDescr = itemTranslation.Description
};
I get "Cannot convert type 'string' to 'int'".
Some advice? I cannot change the version of entity framework and the models.