I have following objects:
public class City
{
public int CityId { get; set; }
public string Name { get; set; }
public virtual ICollection<CityTranslation> CityTranslations { get; set; }
}
public class CityTranslation
{
public int CityId { get; set; }
public string LanguageCode { get; set; }
public string Translation { get; set; }
public virtual City City { get; set; }
}
City table contain default language value in Name field other translations are in CityTranslation table.
I am having problem to get value by language from this.
I am trying to execute following:
public virtual IEnumerable<City> GetAllByLanguage(string language)
{
if (language != "en")
{
var data = from c in context.Cities
join t in context.CityTranslations
on c.CityId equals t.CityId
&& t.LanguageCode == language
select c;
}
else
{
return dbSet.ToList();
}
}
But I am gettings compile error.
Operator '&&' cannot be applied to operands of type 'int' and 'bool'
plus some casting errors.
What should I do to get value from translation table?
Others have spotted the issue, but you shouldn't even need the join - EF will handle it:
var data = from t in context.CityTranslations
where t.LanguageCode == language
select t.City;
对于多个值的联接,您需要使用匿名类型创建复合键,因此联接语句必须类似于
on new {t.CityId, t.languageCode} equals new {c.CityId, language}
The second predicate should not be part of the join:
var data = from c in context.Cities
join t in context.CityTranslations
on c.CityId equals t.CityId
where t.LanguageCode == language
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.