tags:

views:

58

answers:

3

I have this simple query ->

var games =
from p in _oListGames
where p.Visitor == team.ID
select p

I would like to order this select with the COUNT of total games that the p.Visitor has from that list(_oListGames)

How would I do that?

I would like to order the var "games" by the numbers of visitor games they have currently.

+1  A: 

UPDATE: OK, I think I understand what you're going for now. Here's one option that I think might work:

var gameCounts = _oListGames
    .Where(p => p.Visitor == team.ID)
    .GroupBy(p => p.Home)
    .Select(g => new { Opponent = g.Key, Count = g.Count() })
    .OrderByDescending(x => x.Count);

This is basically like Mark's answer but it actually gives you the counts in the results (instead of just ordering by count).

Dan Tao
Not my downvote, but I read it differently, he wants to order it by the count, there are multiple results in the list. E.g. the team that played the visit 20 times comes before the one that played then 10 times, etc.
Nick Craver
A: 
var games = from p in _oListGames where p.Visitor == team.ID select p;
int count = games.Count();

or you can do this.

int count = _oListGames.Count(p=>p.Visitor == team.ID);

to sort it by count of team.ID

var sortGames = games.OrderBy(s => s.Visitor.Count());
rob waminal
+2  A: 

I'm assuming you mean order by number of visitor games that each game's home team had:

var games =
from p in _oListGames
where p.Visitor == team.ID
orderby _oListGames.Count(g => p.Home == g.Visitor) 
select p;
Mark Cidade
I receive this error : " Cannot implicitly conver type long to bool."
Marc789
I fixed my answer.
Mark Cidade
Worked, thanks!
Marc789