c# - LINQ to SQL,从继承类中选择

标签 c# sql sql-server linq inheritance

使用 LINQ to SQL 和对象关系设计;假设我们有一个 class Animal,它有两个继承的类,名为 CatDog。我们可以轻松加载“Animal”类及其属性:

var animals = from a in dataContext.Animals
              select a;

但是,当尝试加载CatDog 时,dataContext 既没有Cats 也没有Dogs 的属性.如何从表Animal 中加载CatDog 以及它们自己的额外属性?

提前致谢。

最佳答案

使用 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/

相关文章:

c# - 在面板中创建 KeyDown 事件时遇到问题

sql - SQL Server varchar(max) 中的字符数?

sql - 如何在 SqLite View 语句上放置 where 条件?

sql-server - 内部连接返回更多行然后存在于表中

c# - Entity Framework 一对多自引用关系

c# - python中的yield == C#中的yield return?

c# - 如何使用 colorstream 在 WPF kinect 应用程序中流式传输图像上的 RGB 数据?

sql - 在 spark sql 中用 LIMIT 描述

sql-server - 在 Amazon RDS 上创建 SQL Server 数据库的 .bak 备份时出现权限错误

sql - 创建/操作索引时的默认 SET 语句?