Sorting problem with Linq
-
I have the following code : DataClassesDataContext dc = new DataClassesDataContext(); IOrderedQueryable myObject; public IOrderedQueryable ZoekDossier(string pDossierNr, string pKlantNaam, string pDeurwNaam, string pOpdrNaam, Int32 pSector, string pSortColumn) { if (string.IsNullOrEmpty(pDossierNr) & !string.IsNullOrEmpty(pKlantNaam) & string.IsNullOrEmpty (pDeurwNaam) & string.IsNullOrEmpty(pOpdrNaam)) { switch (pSortColumn) { case "Dossiernr": var myRecordSetDosNr = from invordering in dc.Invorderings join invo_klan in dc.Invordering_Klants on invordering.INVO_ID equals invo_klan.INKL_INVO_ID join klant in dc.Klants on invo_klan.INKL_KLAN_ID equals klant.KLAN_ID where klant.KLAN_NM.StartsWith(pKlantNaam) & invordering.INVO_SECT_ID == pSector select new { Dossiernr = invordering.INVO_DOS_NR, Hoofdschuld = invordering.INVO_BDR_HFD, Intrest = invordering.INVO_BDR_INT, Kosten = invordering.INVO_BDR_KOS, Klantnaam = klant.KLAN_NM }; myObject = myRecordSetDosNr.OrderBy(p => p.Dossiernr); break; ... When I bind this to a GridView the sorting is done but not in a correct way... The result is : 4, 10, 8, 12, ... So it doesn't sort the right ascending way. Anyone has an idea ? Dossiernr is a varchar(50) field in de SQL Server 2005 database Thx
-
I have the following code : DataClassesDataContext dc = new DataClassesDataContext(); IOrderedQueryable myObject; public IOrderedQueryable ZoekDossier(string pDossierNr, string pKlantNaam, string pDeurwNaam, string pOpdrNaam, Int32 pSector, string pSortColumn) { if (string.IsNullOrEmpty(pDossierNr) & !string.IsNullOrEmpty(pKlantNaam) & string.IsNullOrEmpty (pDeurwNaam) & string.IsNullOrEmpty(pOpdrNaam)) { switch (pSortColumn) { case "Dossiernr": var myRecordSetDosNr = from invordering in dc.Invorderings join invo_klan in dc.Invordering_Klants on invordering.INVO_ID equals invo_klan.INKL_INVO_ID join klant in dc.Klants on invo_klan.INKL_KLAN_ID equals klant.KLAN_ID where klant.KLAN_NM.StartsWith(pKlantNaam) & invordering.INVO_SECT_ID == pSector select new { Dossiernr = invordering.INVO_DOS_NR, Hoofdschuld = invordering.INVO_BDR_HFD, Intrest = invordering.INVO_BDR_INT, Kosten = invordering.INVO_BDR_KOS, Klantnaam = klant.KLAN_NM }; myObject = myRecordSetDosNr.OrderBy(p => p.Dossiernr); break; ... When I bind this to a GridView the sorting is done but not in a correct way... The result is : 4, 10, 8, 12, ... So it doesn't sort the right ascending way. Anyone has an idea ? Dossiernr is a varchar(50) field in de SQL Server 2005 database Thx
-
No that was too easy :p I wanted to use "late binding" for the "order by" clause and I solved this with the LINQ Dynamic Query Library. You can find the link here : http://aspalliance.com/1569\_Dynamic\_LINQ\_Part\_1\_Using\_the\_LINQ\_Dynamic\_Query\_Library.3 Just add the "Dynamic.cs" class to your data/business layer en build it. Then it is possible to use the OrderBy method where you can pass a string value.
-
No that was too easy :p I wanted to use "late binding" for the "order by" clause and I solved this with the LINQ Dynamic Query Library. You can find the link here : http://aspalliance.com/1569\_Dynamic\_LINQ\_Part\_1\_Using\_the\_LINQ\_Dynamic\_Query\_Library.3 Just add the "Dynamic.cs" class to your data/business layer en build it. Then it is possible to use the OrderBy method where you can pass a string value.
Mr programboy wrote:
I wanted to use "late binding" for the "order by" clause
If the list is going to be data-bound in a WinForms UI, another option would be a SortableBindingList like the one illustrated here[^] - this has the added benefit of automatically giving the end-user re-sort capabilities at runtime without you touching a further line of code..
-
Mr programboy wrote:
I wanted to use "late binding" for the "order by" clause
If the list is going to be data-bound in a WinForms UI, another option would be a SortableBindingList like the one illustrated here[^] - this has the added benefit of automatically giving the end-user re-sort capabilities at runtime without you touching a further line of code..
I'm not using a WinForm but thanks :-) This topic can be closed.