views:

39

answers:

2

Hello everybody

Let's say we have tables named A, B, C . C derived from B and B derived from A. So if i want to get C table, OfType(C) will bring result as expected. But if i wrote OfType(B) result will include entries of table C.

How i could get result just for B ? Is there better solution of Entity Framework ?

+2  A: 

There's probably a better/faster/sexier way to do this, but you could always do something with LINQ:

var results = yourCollectionOfBsAndCs.Where(o => !(o is C));
Tomas Lycken
i tried to wrote linq query for it When i was dealing with that while learning but i couldn't write simple query as u did
Freshblood
@Freshblood: What happened when you tried this? What errors do you see?
Tomas Lycken
Sorry test took time . Now i tested it and it works as expected :)
Freshblood
Freshblood
@Freshblood - as you can see on the name of my collection, I assumed you had already filtered out the A's from the collection. However, if you provide more information on your database structure and what discenrs A's, B's and C's, it will be easier to find a more effective solution.
Tomas Lycken
A: 

If we try to exclude inherited classes so we have to write all of them and we should to update our code if there will be another inherited classes in future. So instead of them this code will be better solution.

//collection include classes A and inherited classes from A.
collection.Where(o => o.GetType() == typeof(A)); 
Freshblood
But there is still problem in here. When i try to loop result in foreach i got exception "LINQ to Entities does not recognize the method 'System.Type GetType()' method, and this method cannot be translated into a store expression."
Freshblood