One of the things that you have beaten into you as a junior developer is that you never, ever do a “SELECT *” on a data set, as it is unreliable for several reasons.
Since moving over to Linq (firstly Linq to SQL and then the Entity Framework), I have wondered if the Linq equivalent is equally frowned upon?
Eg
var MyResult = from t in DataContext.MyEntity
where t.Country == 7
select t;
Should we be selecting into an anonymous type with just the fields we want explicitly mentioned, or is the catch all select now acceptable for LinqToSql et al because of the extra stuff surrounding the data they provide?
Regards
Moo
It’s not frowned upon, it’s determined by your use case. If you want to update the result and persist it then you should select
t, however if you don’t want to do that and are just querying for display purposes you can make it more efficient by selecting the properties you want:This is for a few reasons. The population of an anonymous type is slightly faster, but more importantly it disables change tracking…because you can’t persist an anonymous type, there’s no need to track changes to it.
Here’s an excellent rundown of the common performance areas like this that’s great when starting out. It includes a more in-depth explanation of the change tracking I just described as well.