Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Type Handler for List<int> To catch empty lists to avoid Full Table Scan #2034

Open
InvincibleDRT opened this issue Jan 26, 2024 · 1 comment

Comments

@InvincibleDRT
Copy link

Any tips on adding Custom Type Handler for List to catch null or empty lists and throw exception. Getting lot of timeouts because of these empty lists. I tried doing it like this

public class EmptyListTypeHandler<T> : SqlMapper.TypeHandler<List<T>> { public override void SetValue(IDbDataParameter parameter, List<T> value) { if (value?.Any() == false) { throw new ArgumentException("List cannot be empty"); } parameter.Value = value; } public override List<T> Parse(object value) => (List<T>)value; }

But keeps on getting errors for "IN" clause.

Any tips? TIA

@HugoRoss
Copy link

Assuming you have an Id column and a list of Id values you like to use as filter, you could do something like this:

list with multiple entries => WHERE Id IN (1,2,3,5,8,13) (with OPTION RECOMPILE in SQLServer)
list with 1 entry => WHERE (Id = @Id) (and setting the value of @id)
empty list => WHERE (1 = 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants