简体   繁体   English

实体框架-如何查询多个表并选择要显示的列

[英]Entity Framework - how to query over multiple tables and choose columns to display

I have a database with multiple tables and some stored procedures that query if to get specific data from various tables in order to display desired information. 我有一个包含多个表和一些存储过程的数据库,这些存储过程查询是否从各种表中获取特定数据以显示所需的信息。 Find one example of a stored procedure bellow: 在下面找到一个存储过程的示例:

SELECT DISTINCT             
    ROW_NUMBER() OVER (ORDER BY dbo.[table1].ReportedDate DESC) AS rowNumber,
    dbo.[table1].[id],
    dbo.[table1].caseReference,
    dbo.[table2].organisationName AS customerName,
    dbo.[table3].assetRegistration,
    dbo.[table4].surname
FROM dbo.[table1] WITH (NOLOCK)
LEFT JOIN   dbo.[table2] with (NOLOCK)
ON          dbo.[table2].JobId = dbo.[table1].[id]
LEFT JOIN   dbo.[table3] WITH (NOLOCK)
ON          dbo.[table3].id = dbo.[table2].[JobServiceId]
LEFT JOIN   dbo.[table4] WITH (NOLOCK)
ON          dbo.[table4].[jobID] = dbo.[table1].[id]
WHERE (table1.caseReference LIKE @caseReference+'%')

I want to move from using such stored procedures to a more code based approach using entity framework. 我想从使用这种存储过程转变为使用实体框架的基于代码的方法。 How can I recreate a query like the one above using Linq query over the dbContext classes mapped to the database? 如何在映射到数据库的dbContext类上使用Linq查询来重新创建上述查询?

I am mostly having problems in figuring out how to choose the data I want to be returned from each table and how to put it all together. 我在弄清楚如何选择要从每个表中返回的数据以及如何将所有数据组合在一起时遇到了很多问题。

you can do it easly Linq query or lambda expressions over the dbContext classes mapped to the database. 您可以通过映射到数据库的dbContext类轻松地执行Linq查询lambda表达式

check this it would be help you 检查这将对您有帮助
Entity Framework Join 3 Tables 实体框架联接3个表
How to join multiple tables? 如何联接多个表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM