Ling Lambda Orderby

How to pass function to Linq's Orderby

Page 1 of 1

4 Replies - 11837 Views - Last Post: 24 January 2010 - 10:40 PM Rate Topic: -----

#1 bhogsett  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-January 10

Ling Lambda Orderby

Posted 24 January 2010 - 01:41 PM

I am trying to pass a lambda function to Linq's orderby and cannot get it to work.

The relevant code is:

		Dim myordering = Function(m) m.Count
		If isAlphaOrder Then
			myOrdering = Function(m) m.Key  'Order by Alphabet
		End If
		Dim theOutput As New Dictionary(Of String, Integer)

		For Each g In searchResults.Cast(Of Match)() _
			.GroupBy(Function(m) m.Value) _
			.OrderByDescending(myOrdering) _
			.ThenBy(Function(m) m.Key)
			theOutput.Add(g.Key, g.Count)
		Next



searchResults is a MatchCollection

I have tried many variations of the definition of myordering with no sucess.

The error message is:

Public member 'Count' on type 'Grouping' not found.

The for each works fine if I make orderdescending as:

.OrderByDescending(Function(m) m.Count) _



or
.OrderByDescending(Function(m) m.Count) _



Suggestions?

Bill

Is This A Good Question/Topic? 0
  • +

Replies To: Ling Lambda Orderby

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Ling Lambda Orderby

Posted 24 January 2010 - 04:44 PM

Could you explain what the result is you are trying to achieve, with example data. (before & after)
Was This Post Helpful? 0
  • +
  • -

#3 bhogsett  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-January 10

Re: Ling Lambda Orderby

Posted 24 January 2010 - 05:12 PM

View PostAdamSpeight2008, on 24 Jan, 2010 - 03:44 PM, said:

Could you explain what the result is you are trying to achieve, with example data. (before & after)


Adam, the output if the for each is this:

		For Each g In searchResults _
			.Cast(Of Match)() _
			.GroupBy(Function(m) m.Value) _
			.OrderByDescending(Function(m) m.Count) _
			.ThenBy(Function(m) m.Key)
			theOutput.Add(g.Key, g.Count)
		Next



Is sorted by number of times an item (words in my case) is in search results and looks like this:

to 14
of 9
the 9
and 6
count 6
Do 5
for 5
life 5
Not 5
that 5
All 4
be 3
Count 3
in 3
it 3
Maybe 3
on 3
or 3
our 3
We 3
with 3
Work 3

etc.

If the orderbydescending is changed to:
.OrderByDescending(Function(m) m.Key)



The results are sorted in reverse alphabetical order and look like this:

You 2
you 2
years-Lawyers 1
years 1
would 2
Working 1
Work 3
work 2
With 1
with 3
Why 1
-Who 1
Who 2

etc.

The test file that is turned into searchResults (a MatchCollection) is attached, if that helps.

Bill

Attached File(s)

  • Attached File  Test.txt (2.29K)
    Number of downloads: 175

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Ling Lambda Orderby

Posted 24 January 2010 - 05:45 PM

It would a lot easier to write 2 separate linq queries.
  Dim alphaordering As Boolean = True
  Dim q1
  If alphaordering Then
   q1 = From re3 In (From re2 In (From re As Match In rm Group re By re.Value Into Group) Order By re2.Value Descending) Select re3.Value, re3.Group.Count
  Else
   q1 = From re3 In (From re2 In (From re As Match In rm Group re By re.Value Into Group) Order By re2.Group.Count Descending) Select re3.Value, re3.Group.Count

  End If


Was This Post Helpful? 0
  • +
  • -

#5 bhogsett  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-January 10

Re: Ling Lambda Orderby

Posted 24 January 2010 - 10:40 PM

View PostAdamSpeight2008, on 24 Jan, 2010 - 04:45 PM, said:

It would a lot easier to write 2 separate linq queries.
  Dim alphaordering As Boolean = True
  Dim q1
  If alphaordering Then
   q1 = From re3 In (From re2 In (From re As Match In rm Group re By re.Value Into Group) Order By re2.Value Descending) Select re3.Value, re3.Group.Count
  Else
   q1 = From re3 In (From re2 In (From re As Match In rm Group re By re.Value Into Group) Order By re2.Group.Count Descending) Select re3.Value, re3.Group.Count

  End If



Thanks, I'll have to study your code to understand it.

Bill
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1