简体   繁体   中英

How do I get just the base table rows when using Table-per-Type with Entity Framework?

I have the following tables mapped in Entity Framework 4.2 as table-per-type.

Gear (ID, Name, Description) // base table
Weapon (ID, Damage, Bonus) // FK to Gear table.
Armor (ID, Kinetic, Energy) // FK to Gear table.

I can get the weapons and gear separately as such:

var weapons = db.Gear.OfType<Weapon>(); // Gets just weapons.
var armor = db.Gear.OfType<Armor>(); // Gets just armor.

but I can't figure out how to get just the base rows. I was hoping something like this would work but it still gets all of them.

var basicGear = db.Gear.OfType<Gear>();

尝试

var basicGear = db.Gear.Where(g => !(g is Weapon || g is Armor));

Since you want all the rows that aren't specifically Weapons or Armour. Generally, I would say you could try using the Except method .

var weapons = db.Gear.OfType<Weapon>(); // Gets just weapons.
var armor = db.Gear.OfType<Armor>(); // Gets just armor.
var allOtherStuff = db.Gear.Except(weapons).Except(armor);

Or even

var allOtherStuff = db.Gear.Except(weapons.Union(armor));

However, I'm not aware if Except has a translation to SQL within the Entity Framework (based on some answers on here they suggest it might not but this MSDN page would suggest it might).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM