[英]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. 我有以下表格在实体框架4.2中映射为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 .
一般来说,我会说你可以尝试使用Except方法 。
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). 但是,我不知道Except是否在实体框架内有一个SQL转换(基于这里的一些答案 ,他们建议它可能不会,但这个MSDN页面会暗示它可能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.