使用 LINQ to SQL 和对象关系设计;假设我们有一个 class Animal
,它有两个继承的类,名为 Cat
和 Dog
。我们可以轻松加载“Animal”类及其属性:
var animals = from a in dataContext.Animals
select a;
但是,当尝试加载Cat
或Dog
时,dataContext 既没有Cats
也没有Dogs
的属性.如何从表Animal
中加载Cat
或Dog
以及它们自己的额外属性?
提前致谢。
最佳答案
使用 OfType<T>
:
var dogs = from a in dataContext.Animals.OfType<Dog>()
select a;
对于猫:
var cats = from a in dataContext.Animals.OfType<Cat>()
select a;
如图here
...和一个完整的例子here
关于c# - LINQ to SQL,从继承类中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142465/