DarthVader
DarthVader

Reputation: 55062

Where clause bool fail

class Foo{
        public string FooText { get; set; }
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
        public ICollection<Bar> Bars{ get; set; }
}

class Bar{
        public Foo Foo { get; set; }
        public Language Language { get; set; }
        public string FooText { get; set; } 
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
}

I have a collection of Foo. I want to get Foo and Bar with Language with language Name "English"

fooCollection.Where(x => 
                    x.Translations.Where(q =>
                                         q.Language.LanguageName == "English"));

and this fails. Can not convert Foo to bool.

How can i get this done. Essentially i want to get Foo and Bar whose Language is english.

Upvotes: 0

Views: 163

Answers (2)

Bj&#246;rn
Bj&#246;rn

Reputation: 3418

 x.Translations.Where

returns a collection, not a boolean.

fooCollection.Where(x =>
                    x.Translations.Where(q =>
                                         q.Language.LanguageName == "English")
                    .Count() > 0);

should do the trick.

Edit: Added parenthesis due to user2864740 comment. (But I agree that .Any() is the preferred way.)

Upvotes: 4

Jonas W
Jonas W

Reputation: 3260

.Where returns a collection, it's not a bool change it to .Any()

fooCollection.Where(x => 
                    x.Translations.Any(q => 
                                       q.Language.LanguageName == "English"));

Upvotes: 7

Related Questions