Michel
Michel

Reputation: 23615

Linq: select items in the N side of a 1:N relation

i have a table called Text (what's in a name), and a table called TextTranslations

Text can have multiple texttranslations (texttranslation has a FK textid to text.textid) It also has a property called 'translation'.

Now i have a list of text objects which all have 1 or more texttranslation child objects.

and i want to match all text objects which have a child (texttranslation) with the 'translation' property equal to something.

so in pseudocode it would be:

list.Where(1 or more z.childs.texttranslation contains 'bla')

is that possible?

Upvotes: 3

Views: 264

Answers (1)

Meligy
Meligy

Reputation: 36594

from text in context.Text
    where text.TextTranslations.Any(tt => tt.Translation.Contains("bla"))
select text;

Or

context.Text
    .Where(
        text => text.TextTranslations.Any(tt => tt.Translation.Contains("bla")
     );

Or

(from textTranslation in context.TextTranslations
    where textTranslation.Translation.Contains("bla")
select textTranslation.Text)
.Distinct();

Or

context.TextTranslations
    .Where(textTranslation => textTranslation.Translation.Contains("bla"))
    .Select(textTranslation => textTranslation.Text)
    .Distinct();

Upvotes: 2

Related Questions