1110
1110

Reputation: 6829

Entity framework issue with join two objects

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?

Upvotes: 0

Views: 546

Answers (3)

Charles Mager
Charles Mager

Reputation: 26213

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;      

Upvotes: 2

Gert Arnold
Gert Arnold

Reputation: 109080

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

Upvotes: 1

Hargrovm
Hargrovm

Reputation: 1063

For a join on multiple values you need to create composite key using anonymous types so your join statement needs to be something like

on new {t.CityId, t.languageCode} equals new {c.CityId, language}

Upvotes: 1

Related Questions