Robote
Robote

Reputation: 17

Subtract two numbers in Linq from SQLServer

I want to subtract two numbers from data base table for condition : subtract of all times most less than 8

List<Task> ltask = ProjectDAL.GetAllTasks();

in DB I have Start_Time and End_time

I want to subtract Start_Time from End_time for all end and start value inDB for ex :

Start_Time = 10
End_time = 16 
subtract = End_time - Start_Time = 6

var subtract = ltask.Where(x =>x.End_Time - x.Start_Time);

but is not working

Upvotes: 0

Views: 1555

Answers (1)

Peter B
Peter B

Reputation: 24137

You need to use .Select(...):

var subtract = ltask.Select(x => x.End_Time - x.Start_Time);

Assuming that End_Time and Start_Time are int values, this will return an IEnumerable<int> object. If you enumerate this with e.g. foreach or .ToList(), it will calculate the difference for each of the items in the source list ltask.

Upvotes: 2

Related Questions