Now, as you people can see that i am using orderby clause n my query, which will order all observers in ascending order according to their code. Now its working absolutely fine if all codes are single digits. i.e. (1, 2, 3, 4, 5). but if it finds any two
digit code it ruins up the ascending order like (1, 10, 2, 3, 4). It should be noted that when all codes are in two digits then it also works fine like (10, 11, 12, 13, 14). but if it finds a single digit then it comes up with the thing like this (10, 2, 11,
12, 13).
I've noted that the code causing the problem always comes on second place. and when i order my query in descending order it comes on second last place. Why is that so? I am totally freaking out that what should be the problem?
If you have any confusion regarding my question, or if it is unclear then please dont hesitate to communicate with me.
cYpH3r x3r0
Member
327 Points
107 Posts
LINQ Sorting.
May 11, 2012 11:09 AM|LINK
Hello,
I am making a wpf application in c#. using LINQ.
i am using the below code to get the list of all observers by providing project ID to this funtion (getObservers)
public IEnumerable getObservers(int pid)
{
try
{
var query = from m in dc.Observers
orderby m.ObserverCode
where m.ProjectID == pid
select m;
return query as IEnumerable;
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
return null;
}
}
Now, as you people can see that i am using orderby clause n my query, which will order all observers in ascending order according to their code. Now its working absolutely fine if all codes are single digits. i.e. (1, 2, 3, 4, 5). but if it finds any two digit code it ruins up the ascending order like (1, 10, 2, 3, 4). It should be noted that when all codes are in two digits then it also works fine like (10, 11, 12, 13, 14). but if it finds a single digit then it comes up with the thing like this (10, 2, 11, 12, 13).
I've noted that the code causing the problem always comes on second place. and when i order my query in descending order it comes on second last place. Why is that so? I am totally freaking out that what should be the problem?
If you have any confusion regarding my question, or if it is unclear then please dont hesitate to communicate with me.
Regards
cYpH3r x3r0
Member
327 Points
107 Posts
Re: LINQ Sorting.
May 11, 2012 11:37 AM|LINK
One more thing. observerCode is a string. so when it sorts the observer code then it will also sort numeric values alphabetically
like if we have these values in DB (1, 2, 10, 11, 12, 19)
then it will sort them like this (1, 10, 11, 12, 19, 2)
since its a string so it will assume 2 greater than 19, because it starts with 2 and 19 starts with 1.
i cannot make it integer because i want it to support alphabetic values also (for future if my client wants them).
so can you people give me a workaround for this?
christiandev
Star
8607 Points
1841 Posts
Re: LINQ Sorting.
May 11, 2012 04:22 PM|LINK
possibly might need to implement your own IComparer?
http://stackoverflow.com/questions/771779/optimize-a-listt-sortcomparer
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ Sorting.
May 13, 2012 01:22 AM|LINK
Hello cYpH3:)
You can convert the string to integer and do order by:
var query = from m in dc.Observers.AsEnumerble() orderby Convert.ToInt32(m.ObserverCode) where m.ProjectID == pid select m;christiandev
Star
8607 Points
1841 Posts
Re: LINQ Sorting.
May 13, 2012 08:51 AM|LINK
It needs to handle alphanumeric
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ Sorting.
May 13, 2012 09:32 AM|LINK
You mean observerCode has something that isn't numeric?I think they are of type of string but in the formation of numeric like "numeric string"……:D
christiandev
Star
8607 Points
1841 Posts
Re: LINQ Sorting.
May 13, 2012 10:08 AM|LINK
If you look at the post below the original, it mentions this...
So, I'm guessing they might want to handle 102A, 102B in the future, possibly? Otherwise, that convert to int would be the solution :)
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ Sorting.
May 13, 2012 11:11 AM|LINK
Hello:)
We'd like to ask you a question:Whether your observerCode is purely 1,2,3 or something like 1A,2B,3C?
From the given sample by your description,I think you have pure strings of numeric things……
Reguards!