简体   繁体   中英

c# linq get result from stored procedure that returns a join between two tables

I have a stored procedure that returns a left join between two tables, like:

select * from table a
left join table2 b on a.id = b.a_id

I've added my stored procedure to my edmx file, but the procedure does not appear in my context when I want to get the results, like context.my_stored_procedure . Anyone knows why?

PS: in the stored procedure are actually 12 tables that are joined

You need to create Function Imports using the Model Browser of EDM :

Here are few links which will guide you using walk through of it how to do it:

http://msdn.microsoft.com/en-us/library/vstudio/bb896231%28v=vs.100%29.aspx http://www.entityframeworktutorial.net/data-read-using-stored-procedure.aspx http://programmaticponderings.wordpress.com/2012/11/22/first-impressions-of-database-first-development-with-entity-framework-5-in-visual-studio-2012/

By doing this way EDM will create methods in the dbcontext of the Stored Procedures and you can call them as you call a method by passing parameters in method.

You can not use stored procedures like that in LINQ to Entity. But, instead of that you can create VIEW in the database. And after that you can call this view like that:

context.my_view

Or you can join these tables in LINQ using query or method syntax. For example like that:

 var query = from t1 in entity.TABLE1
              join t2 in entity.TABLE2 on t1.COLUMN3 equals t2.COLUMN5
              join t4 in entity.TABLE3 on t1.COLUMN2 equals t4.COLUMN2 into leftouter
              join t5 in entity.TABLE4 on t4.COLUMN1 equals t5.COLUMN7
              from c in leftouter.DefaultIfEmpty()
              select new MyClass()
              {
                 t1.COLUMN1
              };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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