LINQ (Language-Integrated Query) in C#
Enumerable Class (System.Linq)
Enumerable.GroupBy Method (System.Linq)
C# Sort Examples - Dot Net Perls
/// <summary>
/// The function calla query that: 1 groups the cards by Value,
/// chooses each item with a Value once and
/// if there are duplicates adds all the names of the owners the selected item
/// </summary>
/// <param name="cardList"></param>
/// <param name="cardListWithoutDuplicatedCards"></param>
/// <returns></returns>
private bool RemoveSameValueCards(List<Card> cardList,
out List<Card> cardListWithoutDuplicatedCards) {
cardListWithoutDuplicatedCards = _cardsList.GroupBy(x => x.Value)
.Select(g =>
new Card(g.First().Value,
g.Aggregate(new StringBuilder(),
(current, next)
=> current.Append(current.Length == 0 ? "" : ", ")
.Append(next.Owner)).ToString())).ToList();
return !cardListWithoutDuplicatedCards.IsNullOrEmpty();
}
// <summary>
/// Calculates and returns the list with the top 3 most voted cards (CardWithValue struct)
/// </summary>
/// <param name="cardsWithValue">List of all the cards (we need to get them from all the players)</param>
/// <returns>List of the top 3 cards with value</returns>
[Online]
public List<CardWithValue> CalculateSharedPodium(List<CardWithValue> cardsWithValue) {
List<CardWithValue> top3CardsWithValue = cardsWithValue.GroupBy(x => x._card.Value)
.Select(g => new CardWithValue { _card = g.First()._card, _value = g.Sum(x => x._value) })
.ToList();
//TODO Is possible to add other points to the card if it has received special interactions like markers and approvals
foreach (var card in top3CardsWithValue) {
print($"{card._card.Value} {card._value}");
}
return top3CardsWithValue;
}
private bool FindDuplicateCards(List<CardContainer> cardContainers,
out List<CardContainer> cardContainersWitDuplicatedCards) {
//prendo il valore della carta, CardContainer.Card.Value
//lo confronto con le altre e
//restituisco la lista di container che hanno i doppioni al loro interno CardContainer
cardContainersWitDuplicatedCards = (from x in cardContainers
group x.Card.Value by x
into g
where g.Count() > 1
select g.Key).ToList();
return !cardContainersWitDuplicatedCards.IsNullOrEmpty();
}
Randomize the position of the items and take 3
//privtae variable
static Random _random = new Random();
//...
.OrderBy(x=> _random.Next())
.Take(3);
[LINQ Remove duplicates and result item to have sum of quantities](https://sulachiovenda.notion.site/LINQ-Remove-duplicates-and-result-item-to-have-sum-of-quantities-f019a02831264743a12db1c0dc34ae9a)
Filters a sequence of values based on a predicate.