I have this functional Linq-to-Sql statement.
public IEnumerable<int> GetChildIds(IEnumerable<int> selectedParentIds)
{
using (var context = new MyContext())
{
return context.Children
.Where(c => selectedParentIds.Contains(c.parentId))
.Select(c => c.Id)
.ToList();
}
}
It gives me the selected child Id’s, as requested but, I’ve been running SQL Profiler.
This statement appears to send a seperate request to the database for each selectedParentId which seems sub-optimal to me.
Is there a way I can restructure this statement to minimise the traffic with the server? Is there a different approach I should take or, is this just as good as it gets?
EDIT
Thanks for the guidance.
This is a mistake on my interpretation of my trace ouput, Linq-To-SQL make a perfectly reasonable statement for SQLServer 2005 just as it does for SQLServer 2008. The problem is elsewhere.
The same query using LINQ to SQL against SQL 2008 produces a single SQL query. Are you able to test against SQL Server 2008?
For example:
Produces
And the same thing using Entity Framework produces
So without being able to check this against SQL 2005, I’m going to risk it and say, no there’s nothing you can do unless you upgrade the server.