Select to view content in your preferred language

Linq Sort

1490
12
03-11-2011 12:15 PM
JayKappy
Frequent Contributor
I have been trying to find an answer at Silverlight forums and LINQ forums but no luck....

I can sort by "officer" no problem....but I want to sort by the IncidentNumber field which is numberic...Cant seem to get it to sort...

I tried with no success:

  1. Order By CStr(g.Attributes("IncidentNum")) Ascending

  2. Order By CInt(g.Attributes("IncidentNum")) Ascending

  3. Order By CDbl(g.Attributes("IncidentNum")) Ascending

Works for Officer:       
Dim results2 = From g In featureSet
        Order By CStr(g.Attributes("Officer")) Ascending
        Group g By GroupKey = CStr(g.Attributes("Officer")) Into r = Group
        Select New With {Key .Officer = GroupKey, Key .IncidentNums = r.Sum(Function(i) Convert.ToInt32(i.Attributes("IncidentNum")))}


Does NOT work for IncidentNumber (Map Service Field Name = IncidentNum)
Dim results2 = From g In featureSet
        Order By CStr(g.Attributes("IncidentNum")) Ascending
        Group g By GroupKey = CStr(g.Attributes("Officer")) Into r = Group
        Select New With {Key .Officer = GroupKey, Key .IncidentNums = r.Sum(Function(i) Convert.ToInt32(i.Attributes("IncidentNum")))}
0 Kudos
12 Replies
JayKappy
Frequent Contributor
Anyoen any ideas....The field "IncidentNum" is numeric...I am Creating the "IncidentNums" (with an s) in the LINQ query...but wont sort on the IncidentNums, I can only get it to sort by the text field Officers....
uggggggggggggg
0 Kudos
JenniferNery
Esri Regular Contributor
Is this still the same sample from your previous thread? I was able to sort using Linq in C#. You need to apply type cast on the attribute value from object, it should be int or double, whichever matches your field data type. If you cast it to string, then the sort will be 1, 12, 4, instead of 1, 4, 12.
0 Kudos
JayKappy
Frequent Contributor
Is this still the same sample from your previous thread? I was able to sort using Linq in C#. You need to apply type cast on the attribute value from object, it should be int or double, whichever matches your field data type. If you cast it to string, then the sort will be 1, 12, 4, instead of 1, 4, 12.


Yea I was trying to find the old entry...I cant find it...I think it was in one that a bunc h of issues were dealt with....Still triyng to find it....Looking up Type Cast right now...
0 Kudos
JenniferNery
Esri Regular Contributor
0 Kudos
JayKappy
Frequent Contributor
Didnt really want to post there...as the post was to get the LINQ query working....it was working...
Dindnt want to get lost in 5 issues in the same forum...

I was thinking soemthing like this?  But no go....

Order By CType(g.Attributes("IncidentNum"), Integer) Descending

AND

Order By DirectCast(g.Attributes("IncidentNum"), Integer) Descending

AND

Order By TryCast(g.Attributes("IncidentNum"), String) Descending

Right Path?
0 Kudos
JayKappy
Frequent Contributor
Again not sure by what you mean You need to apply type cast on the attribute value from object
I have looked this up for examples and as you can see above this is all I am really coming up with...Not really following here....I have been all over the interenet here tryign to figure this out....
0 Kudos
JenniferNery
Esri Regular Contributor
Did you use the attached VB solution that is equivalent to the C# code I posted in your old forum?
0 Kudos
JayKappy
Frequent Contributor
Yea but there was nothing in there about sorting
This is where I am having my problems...I can sort the text but not the integer...I have scoured the internet and cant find anything...I am unfamilar with TypeCast, DirectCast syntax in this instant...

VB example you posted
Dim results = From g In graphicCollection Group g By GroupKey = CStr(g.Attributes("Officer")) Into r = Group Select New With {Key .Officer = GroupKey, Key .Incidents = r.Sum(Function(i) Convert.ToInt32(i.Attributes("Incident")))}
For Each item In results
MessageBox.Show(String.Format("Officer {0} has {1}", item.Officer, item.Incidents))
Next item


This is my appempt at sorting...I assume that I should be grabbing the Actual Field Name (incidentNum) instead of the created attribute (IncidentNums with an s)
Dim results2 = From g In featureSet
        Order By CStr(g.Attributes("IncidentNum")) Ascending
        Group g By GroupKey = CStr(g.Attributes("Officer")) Into r = Group
        Select New With {Key .Officer = GroupKey, Key .IncidentNums = r.Sum(Function(i) Convert.ToInt32(i.Attributes("IncidentNum")))}


Variations tried
Order By CType(g.Attributes("IncidentNum"), Integer) Descending
Order By DirectCast(g.Attributes("IncidentNum"), Integer) Descending
Order By TryCast(g.Attributes("IncidentNum"), String) Descending
0 Kudos
JenniferNery
Esri Regular Contributor

  Dim results = From g In featureSet
                        Group g By GroupKey = CStr(g.Attributes("Incident")) Into r = Group
                        Select New With {Key .Incident = GroupKey, Key .IncidentNums = r.Sum(Function(i) Convert.ToInt32(i.Attributes("IncidentNum")))}


From your last post in the old thread the code above worked for you. Can't you just use Convert.ToInt32() then?
0 Kudos