Use of Expression<Func<Entity, bool>> problem

Apr 13, 2015 at 3:46 AM
Hi, I want to use expression methods to reuse some queries but I got an error.

This first ligne of code return the correct number of rows but have null value inside.
var iGotTwoEmptyRow = currentRepository.Query()
                .Select(x => x.Translation.TranslationTexts.AsQueryable().Where(expression).FirstOrDefault()).ToList();
This ligne of code return the correct number of rows with the good values inside.
            var iGotTwoCorrectRow = currentRepository.Query()
                .Select(x => x.Translation.TranslationTexts.AsQueryable().Where(x => x.LanguageId == 1).FirstOrDefault()).ToList();
The usage of the expression be the difference.

Here is my code for the expression variable:
Expression<Func<TranslationText, bool>> expression;
            if (string.IsNullOrEmpty(neutralText))
                expression = FindLocalizedText(languageId, neutralText);
            else
                expression = FindLocalizedTextByLanguage(languageId);
public static Expression<Func<TranslationText, bool>> FindLocalizedTextByLanguage(int languageId)
        {
            return translationText => translationText.LanguageId == languageId;
        }
        public static Expression<Func<TranslationText, bool>> FindLocalizedText(int languageId, string neutralText)
        {
            return FindLocalizedTextByLanguage(languageId).And(translationText =>
                 translationText.NeutralText == neutralText);
        }
I can't find the reason why it doesn't work and return null value with correct number of row. Could you help me please?

Thank you,

David
Apr 13, 2015 at 3:53 AM
The problem was because of this...
Expression<Func<TranslationText, bool>> expression;
            if (string.IsNullOrEmpty(neutralText))
                expression = FindLocalizedText(languageId, neutralText);
            else
                expression = FindLocalizedTextByLanguage(languageId);
Should be this instead :
Expression<Func<TranslationText, bool>> expression;
            if (string.IsNullOrEmpty(neutralText))
                expression = FindLocalizedTextByLanguage(languageId);
            else
                expression = FindLocalizedText(languageId, neutralText);
Marked as answer by vialeda on 4/12/2015 at 8:53 PM