I am currently having to work on a project which uses linq2sql as its database accessing framework, now there are a lot of linq queries which basically do the following:
var result = from <some_table>
join <some_other_table>
join <another_table>
select <some_other_domain_model> // This is a non linq2SQL poco
return result.Where(<Some_Predicate>);
So for example assume you read 3 tables, and then collate the contents into one big higher level model, for sending to a view. Now ignore the mixing of domains, as that doesn't bother me too much, its the final where clause which does.
Now I have not used Linq2Sql much before so would I be right in saying what is going to happen is:
- Generate SQL based off the from, join, join, select linq
- Retrieve all rows
- Map all this data into one big model (in memory)
- Loop through all models and then return only the applicable ones
As this is the crux of my question, it would make sense in my mind if the above flow is what would happen, but it has been debated by people who apparently know the framework a lot better than the 4th step is somehow factored into the SQL generation so it will not be pulling back all records, but I dont know how it could be doing that as it NEEDS all the data up front to populate this which it then applies a separate where clause on, so I assume by the 4th point the rows have all been read and are already in memory.
I am trying to push for them to move their where clause into the linq so that it filters out un-needed records at the database level, however I was wondering if anyone can advise as to if my assumptions above are right?
== Edit ==
Have added comment to draw more attention to the fact that the is not a linq2sql generated object and is some random poco hand rolled elsewhere, just to narrow down where my main focus is on the context of the question. As the question is LESS about "does it matter where I put the where clause"
and more about "Does the where clause still get factored into the underlying query when it is applied to a non linq2sql object generated from a linq2sql query"
.
Here is another more concise example of what I mean hopefully drawing the point more towards where my lack of understanding is:
/*
I am only going to put auto properties into the linq2sql entities,
although in the real world they would be a mix of private backing
fields with public properties doing the notiftying.
*/
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_1")]
public class SomeLinq2SqlTable1
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_1_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_2")]
public class SomeLinq2SqlTable2
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_2_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_2_name", AutoSync=AutoSync.OnInsert, DbType="Varchar NOT NULL", IsPrimaryKey=false)]
public string Name {get;set;}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_3")]
public class SomeLinq2SqlTable3
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_3_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_3_other", AutoSync=AutoSync.OnInsert, DbType="Varchar NOT NULL", IsPrimaryKey=false)]
public string Other {get;set;}
}
/*
This is some hand rolled Poco, has NOTHING to do with Linq2Sql, think of it as
a view model of sorts.
*/
public class SomeViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public string Other {get;set;}
}
/*
Here is psudo query to join all tables, then populate the
viewmodel item from the query and finally do a where clause
on the viewmodel objects.
*/
var result = from // Linq2SqlTable1 as t1
join // Linq2SqlTable2.id on Linq2SqlTable1.id as t2
join // Linq2SqlTable3.id on Linq2SqlTable1.id as t3
select new ViewModel { Id = t1.Id, Name = t2.Name, Other = t3.Other }
return result.Where(viewModel => viewModel.Name.Contains("some-guff"));
So given the example above, will the final Where statement be factored into the underlying query, or will the where on the viewModel cause a retrieval and then evaluate in memory?
Sorry for the verbosity to this question but there is very little documentation about it, and this is quite a specific question.