How to get list with itens of base class type only?

Thread is closed for posting
2 posts, 0 answers
  1. Filipe Peixinho
    Filipe Peixinho avatar
    41 posts
    Member since:
    May 2006

    Posted 03 Jun 2014 Link to this post


    I like to know if it is possible to return a list of items that only exist in the base type of a persistent class.

    For example, assuming that Person is base for Musician:

    1.public void Test()   
    3.    var res1 = Scope.Extent<Musician>().ToList(); //Returns all musicians
    4.    var res2 = Scope.Extent<Person>().ToList(); //Returns all persons including musicians

    My goal is to query only persons that don’t are musicians or other inherited classes of Person. I want only persons. 

    I know thatI could make a ‘not in’ clause (Person not in Musician) or create a new empty Class Based on persons, but I wonder if there is some easy way to achieve these purpose.

  2. Ady
    Ady avatar
    589 posts

    Posted 06 Jun 2014 Link to this post

    Hi Filipe,

     case you want only Person instances then you can obtain the OpenAccess type id of the Person type and check against it  
    Here is the code to do it.

    //obtain metadata for Person type
    var md = context.Metadata.PersistentTypes.Where(p => p.FullName == typeof(Product).FullName).Single();
    //query to get only persons
    var q = from c in context.GetAll<Product>()
                where c.FieldValue<int>(SymbolicFieldName.ClassId) == md.ClassId.Value
                select c;

    Do get back in case you need further assistance.

    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top