This is a migrated thread and some comments may be shown as answers.

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

1 Answer 34 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Filipe Peixinho
Top achievements
Rank 1
Filipe Peixinho asked on 03 Jun 2014, 10:31 AM
Hi!

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()   
2.{         
3.    var res1 = Scope.Extent<Musician>().ToList(); //Returns all musicians
4.    var res2 = Scope.Extent<Person>().ToList(); //Returns all persons including musicians
5.}

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.

Thanks

1 Answer, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 06 Jun 2014, 01:16 PM
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.

Regards,
Ady
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
LINQ (LINQ specific questions)
Asked by
Filipe Peixinho
Top achievements
Rank 1
Answers by
Ady
Telerik team
Share this question
or