0

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;
Firo
  • 30,626
  • 4
  • 55
  • 94
  • I take it `Stock` is a property defined in `Car`. Does `session.QueryOver().Where(w=>w.InStock)` not work? – Rippo Jul 19 '12 at 13:47
  • Is this your actual domain? It doesn't look like a good use case for inheritance. – Diego Mijelshon Jul 19 '12 at 15:32
  • If property you want to filter by is in your `car` class then it is possible – Rippo Jul 19 '12 at 15:38
  • @DiegoMijelshon my domain is actually quite similar. Dependent on the type there is a lot of business logic attached and many columns are only valid for specific types. However i have a use case where i should filter on the existing records in the database and want to show only options which actually are in the database – Firo Jul 19 '12 at 19:34

1 Answers1

1

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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • actually the schema is a classic TPH with half the columns common for all rows and then dependent on a type column columns for individual rows. Since even one-to-many are dependent on the type i thought it would be a good idea. – Firo Jul 19 '12 at 19:31