How the rewrite this IN condition logic to handle in best performance for dotnet core webapi?

Elaya Raja 96 Reputation points
2022-06-06T18:49:53.897+00:00

How the below logic handle in best way for dotnet core webapi?

//Query is like * where JoinDateTime>@startDate AND JoinDateTime<@endDate AND EmpId IN (@EmpId )

var result = await _database.QueryAsync<Employee>(query,
new
{
startDate,
endDate,
EmpId = listOfEmployeeID.ToArray() // Empid is integer and always passing more than 1000+
});

How to split this where IN condition into easy way to avoid the performance issue.?

Thanks
Jo

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2022-06-06T20:11:41.787+00:00

    if you are using sql parameters then each in item is a separate parameter:

    in (@EmpId1, @EmpId2)

    typically you build the in clause with a loop, and add the parameters to the command

    with EF linq, you can use the contains and pass an array

    EmpId.Contains(listOfEmployeeID)