Hi I have this bit of linq code
var fp = lnq.attaches.First(a => a.sysid == sysid).name;
When profiled it generates the following t-sql
SELECT TOP (1) [t0].[sysid], [t0].[name], [t0].[att_size], [t0].[cid], [t0].[flags], [t0].[contents]
FROM [lntmuser].[attach] AS [t0]
The way I look at it, it is returning like a select *, which will cause the query to perform a table scan rather then use an index. Bad for performance.
How could I select just the name column, like:
SELECT TOP (1)[t0].[name] FROM [lntmuser].[attach] AS [t0]
Thanks in advance
Edit: Broken Glasses Solution profiles as desired
SELECT TOP (1) [t0].[name]
FROM [lntmuser].[attach] AS [t0]
WHERE [t0].[sysid] = @p0
Project to the
name
property before usingFirst()
:This doesn't change the use of an index though - for that your
Where
clause is responsible (in your initial query the lambda you passed toFirst()
). Both queries benefit from an index on thename
column, the second one is just faster because only one column value has to be materialized.