I have a list of items
- John ID
- Matt ID
- John ID
- Scott ID
- Matt ID
- John ID
- Lucas ID
I want to shove them back into a list like so which also means I want to sort by the highest number of duplicates.
- John ID 3
- Matt ID 2
- Scott ID 1
- Lucas ID 1
Let me know how I can do this with LINQ and C#.
Thanks All
EDIT 2 Showing Code:
List<game> inventory = new List<game>();
drinkingforDataContext db = new drinkingforDataContext();
foreach (string item in tbTitle.Text.Split(' '))
{
List<game> getItems = (from dfg in db.drinkingfor_Games
where dfg.game_Name.Contains(tbTitle.Text)
select new game
{
gameName = dfg.game_Name,
gameID = Boomers.Utilities.Guids.Encoder.EncodeURLs(dfg.uid)
}).ToList<game>();
for (int i = 0; i < getItems.Count(); i++)
{
inventory.Add(getItems[i]);
}
}
var items = (from xx in inventory
group xx by xx into g
let count = g.Count()
orderby count descending
select new
{
Count = count,
gameName = g.Key.gameName,
gameID = g.Key.gameID
});
lvRelatedGames.DataSource = items;
lvRelatedGames.DataBind();
This query displays these results:
- 1 hello world times
- 1 hello world times
- 1 Hello World.
- 1 hello world times
- 1 hello world times
- 1 hello world times
- 1 Hello World.
- 1 hello world times
It gives me the count and name, but it doesn't give me the ID of the game....
It should display:
- 6 hello world times 234234
- 2 Hello World. 23432432