none
Linq to Entities Query RRS feed

  • Question


  • I have two classes Language and LanguageTranslation. In fact LanguageTranslation contains translations of Language Codes. I have also the correct EF 4.1 Code First mappings in place.

    eg

    Language: Code: EN, DE, ... LanguageTranslation: Name: English, Englisch etc.

    public class Language
    {
    private Guid Id;
    private string Code;
    private ICollection<LanguageTranslation> LanguageTranslations;
    }

    public class LanguageTranslation
    {
    private Guid Id;
    private string Name;
    private Language Language;
    private Language TranslationLanguage;
    }

    I just want to retrieve the corresponding LanguageTranslation for a Language, what do i need to add/change to the following query? 

    public Language GetByCode(string code)
    {
    return context.Languages.SingleOrDefault(p => p.Code == code);

    }


    Sunday, June 19, 2011 5:51 PM

Answers

  • Hi!

    Sorry for double post, for some reason I didn't see Fernandos' answer untill today...

    But anyway, as of my knowlegde, there isn't any easy way to do what you want.

    I would have done it the other way, based my query on LanguageTranslations and include the Language entity, like this:

    LanguageTranslation langtrans = context.LanguageTranslations.Include("Language").Where(row => row.Code = Eng and row.Language.<something> = <somefilter>).FirstOrDefault();


    --Rune
    Monday, June 20, 2011 7:53 AM

All replies

  • Hi GuyD;

    Try it like this.

    return context.Languages.Include("LanguageTranslations").Where(p => p.Code == code).SingleOrDefault();

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, June 19, 2011 6:58 PM
  • Thanks Fernando but the code I use "return context.Languages.SingleOrDefault(p => p.Code == code);" works

    but I, maybe my question wasn't clear, need an additional criteria:

    the query should only return 1 entity of the 1-n association:

    eg.

    When code = "EN"

    Result should be: Code: EN (from Language) Name: English (from LanguageTranslation)

    When code = "DE"

    Result should be: Code: EN (from Language) Name: Englisch (from LanguageTranslation)

     

    Thanks.

    Guy




    Sunday, June 19, 2011 7:37 PM
  • Hi,

    Have you tried:

    return context.Languages.Include("LanguageTranslations").SingleOrDefault(p => p.Code == code);

    This will eager load LanguageTranslations and return them in the resulting Language object.

    If you only want to return the LanguageTranslations you can do code like this instead:

    public List<LanguageTranslation> GetByCode(string code)
    {
       return context.LanguageTranslations.Where(row => row.Language.Code == code).ToList();
    }

    Hope this helps!


    --Rune
    Sunday, June 19, 2011 9:00 PM

  • Thanks Rune. But I think my question isn't still clear.

    My query should return a Language instance/object that includes 1 LanguageTranslation reference (not references to all respective LanguageTranslations).

    Example:

    A user chooses English as its application language then he/she needs to get 1 Language instance/object:

    Language instance/object:

    Id -> system generated Guid

    Code -> EN

    LanguageTranslation -> a reference to the LanguageTranslation object/instance:

       Id -> system generated Guid

       Language -> the reference to the Language here above

       TranslationLanguage -> in this case reference to the Language here above (Code: EN)

       Name -> English 

     

    Hope my question is clear?

    Thanks.



    Monday, June 20, 2011 6:32 AM
  • Hi!

    Sorry for double post, for some reason I didn't see Fernandos' answer untill today...

    But anyway, as of my knowlegde, there isn't any easy way to do what you want.

    I would have done it the other way, based my query on LanguageTranslations and include the Language entity, like this:

    LanguageTranslation langtrans = context.LanguageTranslations.Include("Language").Where(row => row.Code = Eng and row.Language.<something> = <somefilter>).FirstOrDefault();


    --Rune
    Monday, June 20, 2011 7:53 AM
  • Hi,

    Thanks.

    OK I'll try your suggestion.

    I have a lot of this situations in my application eg Country, CountryTranslation; CustomerType, CustomerTypeTranslation, etc. That's the disadvantage of living in a multi-lingual country :-). To my knowledge that's the correct way to implement multi-lingual: One-To-Many (both in Object Model and Data Model).

     

    Thanks.

    Guy

     


    Monday, June 20, 2011 9:51 AM
  • Hi GuyD,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 30, 2011 6:40 AM
    Moderator