I have the following LINQ-to-SQL query:
from cms in ConsignmentMarginBreakdowns.Where(a => a.BreakdownType == 'S')
from cmc in ConsignmentMarginBreakdowns.Where(a => a.BreakdownType == 'C')
where cms.TripNumber == cmc.TripNumber && cms.Depot == cmc.Depot && cms.TripDate == cmc.TripDate
select new
{
NTConsignment = cms.NTConsignment,
Depot = cms.Depot,
TripNumber = cms.TripNumber,
TripDate = cms.TripDate,
Sales = cms.Value,
Costs = cmc.Value,
Margin = cms.Value - cmc.Value
}
The ‘BreakdownType’ field is of ‘char(1)’ type in SQL Server 2008. For some reason, the query brings back no results as it is. To get it to bring back results, I need to change the top 2 lines to the following, which I imagine lowers the speed of the query:
from cms in ConsignmentMarginBreakdowns.Where(a => a.BreakdownType.ToString() == "S")
from cmc in ConsignmentMarginBreakdowns.Where(a => a.BreakdownType.ToString() == "C")
Why is this?
You say, “The ‘BreakdownType’ field is of
char(1)type in SQL Server 2008″, what means that it’s a “Fixed-length, non-Unicode character data with a length of n bytes” (in your case 1), it actually doesn’t say that it’s a “char” just because of the length of 1.See MSDN Docs.