C# Linq to SQL problem with re-ordering by Code (varchar)
I am building a Payroll application in C# (learning C#) that will integrate into SAP Business 1. When creating tables in SAP, SAP generates two fields automatically in user defined tables. One is Code - (primary key) and another is Name. Both are of type varchar/alphanumeric.
I have this query that returns the results although not in the fashion I want. I.e
Code Name U_Tax_year U_Minimum_pay U_Maximum_pay
0 0 2011 1 1
1 1 2011 2 2
10 10 2011 11 11
11 11 2011 12 12
2 2 2011 3 3
3 3 2011 4 4
4 4 2011 5 5
5 5 2011 6 6
6 6 2011 7 7
7 7 2011 8 8
8 8 2011 9 9
9 9 2011 10 10
This is due to the fact that Code and Name are not integer fields.
Below is my query:
// Get service instances
var nhifTableSlabService = Program.Kernel.Get<INHIFTableSlabService>();
//Query database
var nhiftableSlabs = from tb in nhifTableSlabService.GetAllNHIFTableSlabs()
where tb.U_Tax_year.ToString().Trim().Equals(cb_tax_year.Text.Trim(), StringComparison.CurrentCultureIgnoreCase)
开发者_开发百科 orderby int.Parse(tb.Code.ToString()) descending
select tb;
if (nhiftableSlabs.Any(x => x != null)
{
// Enable datagridview
dataGridView1.Visible = true;
...bla bla
}
How do I get the results, convert the Codes to integers so that I can finally display the results in descending order of code?
Use Convert.ToInt32()
instead of int.Parse
and you are good to go. See here for more details.
精彩评论