c# - 在 LINQ to SQL 中联接多级关系

标签 c# .net linq linq-to-sql

有 3 个表:ParentCategories -> 类别 -> 文章。

  • 父类别(ID、名称)
  • 类别(ID、父类别ID、名称)
  • 文章(ID、caregoryID、名称)

如何使用 LINQ 选择具有指定 parentCategoryID 的所有文章(表文章仅引用 categoryID,而不引用 ParentCategoryID) SQL?

类似这样的事情:

articles = (
    from a in db.Articles
    join c in db.Categories
    on ????????????
    join pc in db.ParentCategories 
    on c.ParentCategoryId equals pc.ID
    ...);

最佳答案

(如果我正确理解您的架构)您可以使用隐式连接策略,例如:

var articles = db.Categories
    .Where(c => c.ParentCategoryID == yourParentCategoryID)
    .SelectMany(c => c.Articles)
    .ToList();

隐式连接要求您有 Associations在 O/R 设计器中的实体之间进行设置。

关于c# - 在 LINQ to SQL 中联接多级关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5008967/

相关文章:

c# - 使用 java 和 WP7 进行 AES 加密

linq - 按最大值分组

c# - 组合的分组算法

c# - Dapper 和 Npgsql 通过使用带有 IN 子句和 List<int> 的 EXPLAIN 抛出异常

.net - Web 角色中的 app.config 未复制到 Azure SDK 1.8 和 Visual Studio 2010 中

c# - 错误 : 'fields' parameter is required for this method

c# - 在 while 循环期间文件正在被另一个进程使用 (C#)

c# - 直接用 LINQ 查询的结果填充 ComboBox

c# - 相对于服务器的 URL

c# - Active Directory 组在创建后无法立即可用