score:1
This probably isn't exactly what you're looking for, since you're comparing object
s and not just int
s, but this could help you get started. Based on this question here: How to get frequency of elements stored in a list in C#.
using System.Linq;
List<int> ids = //
int maxFrequency = 0;
int IDOfMax = 0;
foreach(var grp in ids.GroupBy(i => i))
{
if (grp.Count() > maxFrequency)
{
maxFrequency = grp.Count();
IDOfMax = grp.Key;
}
}
// The object (int in this case) that appears most frequently
// can be identified with grp.key
Update: after rereading the question, it sounds like you need to try to return a new object with the count and the face value from your query.
You can do this like so:
public class FaceCountResult
{
public int Count { get; set; }
public Face FaceValue { get; set; }
public FaceCountResult(int count, Face faceValue)
{
Count = count;
FaceValue = faceValue;
}
}
Then, faceCount
should look something like this:
var faceCount = (from card in hand.Cards
group card by card.Face
into g
let count = g.Count()
orderby count descending
select new FaceCountResult(count, card.Face);
I'm not sure how the Take(2)
part would factor into this, as I don't quite understand that part of the code.
You can then do a switch
on faceCount[0].Count
and use faceCount[0].FaceValue
to get the face value.
Source: stackoverflow.com
Related Articles
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- Using Linq find first object in list sorting by property A, then property B
- Find object in list with given property value then find dictionary value
- Accessing a List multiple times in same code block - Any better approach?
- How to take the Max value from a List of object where the same objects exists with many duplicate rows
- Why is my code returning a list of the same object 4 times?
- Entity-framework code is slow when using Include() many times
- How can I find object in List with Linq?
- C# Compare two list of same object type
- Get how many times an array appears in a List<>
- Find object inside collection of List
- LINQ: single list which consists of multiple properties of same type of object collection
- find object from Tree or List Hierarchy
- How to find same classes in a list with one different value
- Find the List index of the object containing the closest property value
- Best way to join 3 lists and count how many times an ID is in the list using linq
- C# Linq select object in List where int in object child List<int> occurs n times
- Add range to a new list of object within the same command / line
- Find an Object that contains a string in a list of strings using LINQ
- find list elements which are similar to other list elements by 3 properties and then add value from the second one to the first
- Linq into the array then find values in the array and list to combobox
- How to find the last item from list have the same item using linq
- find common DateTime from list of DateTime, if no common then find most common
- Given a list of color objects, find the most/least frequently existing color, and return that as a color object
- Find an object within a list and replace its value
- linq find in which position is my object in List
- Count object with same attributevalues in list of objects with GroupBy
- Find distinct values from a list of object by one field only
- Linq: Find elements of 2 list with different values but same index
- C# LINQ - Find object in list between two properties
- Is there any way to negate a Predicate?
- How to join a one-to-many relationship in Entity Framework?
- Convert two arrays to a new one using LINQ
- How to randomize a list with linq from a saved guid?
- How to set TransactionScope to Read Uncommited by default
- Multiple joins using linq
- linq join with case condition
- Deep filtering in Grouped collection of Entities
- How to Remove unneccessary list using lambda and functional C# paradigm
- LINQ to SQL: Group, Count, Sum. I'm so confused
- Select a particular value from datatable-C#
- How to connect a users table correctly to a Membership table, or should you even do that?
- Linq SELECT AS and CONCAT
- Joins with a Numbers table in Entity Framework
- how to efficiently Comparing two lists with 500k objects and strings
- are these two linq expressions functionally equivalent?
- Why does this foreach loop NOT throw the "collection was modified" exception?
- using Case or if statment in LINQ?
- Search Hierarchical List Recursively
- How to change value of child element based on attribute of child and parent?