【问题标题】:How to join on multiple columns in .NET Entity Framework with extension methods?如何使用扩展方法加入 .NET Entity Framework 中的多个列?
【发布时间】:2014-09-20 00:25:11
【问题描述】:

我正在尝试执行这样的查询:

SELECT *
FROM pd AS p
JOIN wh AS w
ON p.ID = w.ID
AND p.ln = w.ln
AND p.wID = w.WID
WHERE w.zID = '1234'

到目前为止,我可以像这样访问表格:

WEntity entity = new WEntity();
// I can call the entities like this: entity.pd and entity.wh.

我用谷歌搜索并找到了一列的示例,例如。

var data = entity.pd
                 .Join(entity.wh,
                       p => p.ID,
                       w => w.ID,
                       (p, w) => p).Where( ... );

【问题讨论】:

  • 您是否在数据类上设置了集合属性?请给他们看。如果一切都正确相关,则不需要进行 Join。
  • 我没有收藏属性。这些表在应用程序之前就存在了,所以这是首先创建数据库而不是代码。

标签: c# sql .net entity-framework extension-methods


【解决方案1】:

这可能会有所帮助 - 假设这是您所追求的,它有几个连接:

post = (From ca In db.be_Categories Join c In db.be_PostCategory On ca.CategoryID Equals (c.CategoryID) Join p In db.be_Posts On c.PostID Equals (p.PostId) Where p.PostRowID = id Select New PostDTO With {.PostCategory = Nothing, .PostDateCreated = p.DateCreated, .PostGuid = p.PostId, .PostId = p.PostRowID, .PostText = p.PostContent, .PostTitle = p.Title}).FirstOrDefault

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多