0

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?

3
  • t.CityId is int, t.LanguageCode == language returns boolean. And && is logical AND operator. You can't use it wth int and bool. Commented Jul 6, 2014 at 15:45
  • 3
    Don't you mean where t.LanguageCode == language? You can also include the WHERE clause in lambda syntax in the reference to the table you're joining on e.g join t in context.CityTranslations.Where(x => x.LanguageCode == language) on c.CityId equals t.CityId Commented Jul 6, 2014 at 15:50
  • var data = from c in context.Cities join t in context.CityTranslations.Where(x => x.LanguageCode == language) on c.CityId equals t.CityId select c; but I am getting casting error. Commented Jul 6, 2014 at 17:50

3 Answers 3

2

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;      
Sign up to request clarification or add additional context in comments.

9 Comments

Actually, I think this answer is better!
It doesn't return good translation. It still return City with translation from city table. Your answer return it from translation table.
There is no 'City' in the translation table. Both methods achieve the same thing (and probably execute the same SQL) - you're doing a join and returning the 'City' part. Neither will get you a translation unless you include t.Translation in any result.
@CharlesMager I have to reopen this question as I accepted answer a little too fast. Result city from this query still return me City object with default language. Can you help me to get translated object?
@1110 There is no City object with the translated name - what is it you actually want back? It sounds like you just want all CityTranslations that match the LanguageCode, which is a far simpler query (it's as I wrote, but select t rather than select t.City.
|
1

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

Comments

1

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.