How to do “In” a list of values with LINQ

I have always done it the following way.

List<string> listMessageUid = new List<string>() { "100013", "100014", "100015", ... };

using (var uow = new MyUOW())
{
List<XXXXEmail> emails = uow.XXXXEmailRepository.Find(x=> listMessageUid.Contains(x.MessageUid)).ToList();
// do something with
}

But I am curious how efficient the LINQ generated SQL query is.

SELECT 
[Extent1].[Id] AS [Id],
[Extent1].[MessageUid] AS [MessageUid],
[Extent1].[xxxx] AS [xxxx]
FROM [dbo].[CourtEmail] AS [Extent1]
WHERE ([Extent1].[MessageUid] IN ('100013', '100014', '100015', '100016', '100018', '100020', '100025', '100029', '100030', '100069', '100070', '100071', '100072', '100073', '100074', '100076', '100079', '100088', '100089', '100090', '100091', '100092', '100389', '100390', '100392')) AND ([Extent1].[MessageUid] IS NOT NULL)

Conclusion:

I like how LINQ’s Contains behaves like TSQL’s IN.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s