Unnecessary conversion to bigint

I have an employee table with a bigint primary key bigint in the database and an entity data model with the first database approach. The employee class has this structure.

 public partial class Employee { public long Emp_No { get; set; } public string Name { get; set; } public string Family { get; set; } ... } 

I am writing this basic query with Entity Framework

 List<long> ids = new List<long>() {1,2,3,4,5,6} database.Employees.Where(q => ids.Contain(q.Emp_No)).ToList(); 

Create a query as follows:

 SELECT [Extent1].[Emp_No] AS [Emp_No], [Extent1].[Name] AS [Name], [Extent1].[Family] AS [Family], ... FROM [dbo].[Employee] AS [Extent1] WHERE [Extent1].[Emp_No] IN (cast(0 as bigint), cast(1 as bigint), cast(2 as bigint), cast(3 as bigint), cast(4 as bigint), cast(5 as bigint), cast(6 as bigint)) 

As you can see, there is no unnecessary cast to bigint in the query, while both types of the Emp_No and ids array are equal to long , it causes bad Emp_No , especially when the ids array has many elements.

How to remove this redundant listing?

+7
c # sql-server expression entity-framework
source share
2 answers

There is practically no cost in cast(0 as bigint) conversion, but because Emp_No also bigint , if you didn’t have a throw, int will still need to be promoted to bigint in order to be able to do IN , so the actor will happen anyway just behind the curtains.

Run the immediate version of the request yourself in the management studio and get the actual execution plan, and you will still see the conversion in the request plan.

+3
source share

Not sure what you are asking for here, but ..

Change your long to int, and the request should make you int instead of bigint.

 public partial class Employee { public int Emp_No { get; set; } public string Name { get; set; } public string Family { get; set; } .... } 

The long equivalent of bigint. You can read more here: What is equivalent to bigint in C #?

-2
source share

All Articles