cs0815
cs0815

Reputation: 17388

Get objects of certain types

I am trying to get Host objects which have E objects of certain type like this:

return Session.Query<Host>().Where(x => x.E is B).ToList();

This does not seem to work probably because E is of type A and B inherits from A. Could the inheritance be the problem. Some more (simplified) details:

class Host
{
    public A E { get; set; }
}

class B : A
{

}

Any ideas why the above does not work? Thanks.

PS:

Please note that the above should work - I used the wrong class name!

Upvotes: 2

Views: 110

Answers (1)

Jan P.
Jan P.

Reputation: 3297

return Session.Query<Host>().Where(x => x.E.GetType().Equals(typeof(B)).ToList();

This code works in LinqPad:

void Main()
{
    var x = new Host { E = new B() };

    Console.Write(x.E.GetType().Equals(typeof(B)));
}

class A { }
class B : A { }
class Host { public A E { get; set; } }

Upvotes: 1

Related Questions