LINQ TO ENTITIES Joining Multiple Collections
I am in a process of converting some legacy SQL Server Stored Procedures to EF4 using LINQ to Entites. In one scenario i have a Stored Procedure that selects from 3 different Table_Valued Functions like
Select * from Function1 F1 ,Function2 F2, Function3 F3
where F1.ID = F2.ID and
F2.ID = F3.ID (i am showing it simple)
where Function1 F1 ,Function2 F2 and Function3 F3 are 3 Sqlserver Table_Valued Functions that returns table variables.
It would be great if some one could advice what is the best practice for this scenario, Currently what i am doing now is have 3 different C# meth开发者_StackOverflowods(which mimics the same functionality as that of SQL server function) which returns a collection and then join all those 3 collections together to get a final resultset.
Do we have an alternative solution to this as i think by using the above solution the code is calling 3 calls to the database instead of calling everthing together.
I would like the code to call one big SQL, instead of multiple calls.
Thanks in Advance !
The main problem here is that EF doesn't support table valued functions. Your solution have another terrible problem. Using three .NET functions simulating your functions actually calls the database three times (= most probably three sequential roundtrips to the database) but it also pulls whole result set for each function to your application and performs joins in memory of your application server. In most cases this is not acceptable.
The proper solution depends on inner implementation of your functions because perhaps they can be converted either to linq-to-entities or ESQL. If not then you can still use the stored procedure! It is public secret of EF that it doesn't replace stored procedures. They are still part of your arsenal when using it.
精彩评论