Firo
Firo

Reputation: 30813

howto query table per hierarchy which subclasses are present

a simple example

abstract class Car
{
    public virtual long SerialNumber { get; set; }
}

class Mercedes : Car { }

class Fiat : Car { }

class Toyota : Car { }

now i want to query for which types inheriting from car are on stock. How to do this? Or is my design flawed.

example

session.Save(new Mercedes() { SerialNumber = 1 });
session.Save(new Mercedes() { SerialNumber = 2 });
session.Save(new Toyota() { SerialNumber = 1 });

// later
var models = session2.Query<Car>().SelectDistinct(car => car.GetType().Name);

showModelComboBox.Items = models;

Upvotes: 0

Views: 312

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

From what I can see, the following works:

var models = session.Query<Car>().Select(x => x.GetType().Name).ToList();

...and you can apply Distinct later... but it's actually fetching the whole entities. Not good.

It looks like Distinct can't be applied to a GetType expression.

Now, you can do the following:

var models = session.CreateQuery("select c.class from Car c").List();

It will return the raw discriminators, which is not ideal, but it works.

Upvotes: 1

Related Questions