LINQ Select First

后端 未结 1 1763
陌清茗
陌清茗 2021-02-12 12:25

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

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-12 12:27

    Project to the name property before using First():

    var fp = lnq.attaches.Where(a => a.sysid == sysid)
                         .Select(a => a.name)
                         .First();
    

    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 to First()). Both queries benefit from an index on the name column, the second one is just faster because only one column value has to be materialized.

    0 讨论(0)
提交回复
热议问题