开发者

For purpose of EF linq query, is First(c=>cond) equiv to Where(c=>cond).First()?

开发者 https://www.devze.com 2023-03-29 15:15 出处:网络
Is there any hidden subtlety, is one perferred, or is one just a shorter way to write the other?开发者_JS百科

Is there any hidden subtlety, is one perferred, or is one just a shorter way to write the other?

开发者_JS百科
Client = db.Clients.First(c=>c.Name == "Client 1")

and

Client = db.Clients.Where(c=>c.Name == "Client 1").First()


Yes they are equiv.

Simple test it to add them to LINQPad and view the generated Sql statements and on my database I get the exact same Queries generated.

Update :

Below is example of the queries I get against my db.

-- Region Parameters
DECLARE @p0 VarChar(1000) = 'SKY02'
-- EndRegion
SELECT TOP (1) [t0].[MST_SQ], [t0].[EMP_EMPNO]
FROM [EMPLOYEE] AS [t0]
WHERE [t0].[EMP_EMPNO] = @p0
GO

-- Region Parameters
DECLARE @p0 VarChar(1000) = 'SKY02'
-- EndRegion
SELECT TOP (1) [t0].[MST_SQ], [t0].[EMP_EMPNO]
FROM [EMPLOYEE] AS [t0]
WHERE [t0].[EMP_EMPNO] = @p0


I'd say the first is preferred simply because it's shorter - so long as you're aware of what the argument is for. But yes, they should be equivalent - they're equivalent in the "LINQ model of the world" so to speak :)

The same "with a predicate" overload is present for various other LINQ operators - Any, Count, Last etc. Personally I keep forgetting about it, but it's worth remembering if you can :)

0

精彩评论

暂无评论...
验证码 换一张
取 消