Skip to content

Instantly share code, notes, and snippets.

@TWolverson
Forked from Daniel-sims/queryExample.cs
Last active April 8, 2018 12:50
Show Gist options
  • Save TWolverson/92eac536f2296fd1a2e43c7ab1f07423 to your computer and use it in GitHub Desktop.
Save TWolverson/92eac536f2296fd1a2e43c7ab1f07423 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace MCVE
{
class Program
{
static void Main(string[] args)
{
int[] friendlyChampionIds = {14};
int[] enemyChampionIds = {};
// This list includes the teams
// 1. 22,55 vs 14
// 2. 14 vs 15,23
// 3. 22 vs 24
// 4. 1 vs 22, 23
//The linq query should return if BOTH lists of Ids match a matchup
//but if only 1 list has Ids it should find if either team has that Id
//I.E
// if the lists are {14} and {} matches 1 and 3 should return
// if the lists are {1} and {22} match 4 should be returned
// if the lists are {} and {14} should return match 1
// if the lists are {22,55} and {14} match 1 should return
// if the lists are {15,23} and {14} match 2 should be returned
var listOfMatches = GetListOfMatches();
// This query works if there's Ids in both lists, but if you have for example {14} and {} it'll return all matches
// because it's true that list 2 matches all Ids in a team
var fullMatchupMatches = listOfMatches
.Where(q => q.Teams.Zip(new[]{friendlyChampionIds, enemyChampionIds}, (t, n) => n.All(nn => t.Players.Contains(n))).All()
||
q.Teams.Reverse().Zip(new[]{friendlyChampionIds, enemyChampionIds}, (t, n) => n.All(nn => t.Players.Contains(n))).All());
Console.WriteLine("Hello World!");
}
public class Match
{
public IList<Team> Teams { get; set; } = new List<Team>();
}
public class Team
{
public IList<Player> Players { get; set; } = new List<Player>();
}
public class Player
{
public int ChampionId { get; set; }
}
public static IList<Match> GetListOfMatches()
{
IList<Match> matches = new List<Match>();
var matchOne = new Match();
matchOne.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 22},
new Player() {ChampionId = 55},
}
});
matchOne.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 14},
}
});
matches.Add(matchOne);
var matchTwo = new Match();
matchTwo.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 14}
}
});
matchTwo.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 15},
new Player() {ChampionId = 23},
}
});
matches.Add(matchTwo);
var matchThree = new Match();
matchThree.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 22},
}
});
matchThree.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 14},
}
});
matches.Add(matchThree);
var matchFour = new Match();
matchFour.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 1},
}
});
matchFour.Teams.Add(new Team()
{
Players = new List<Player>()
{
new Player() {ChampionId = 22},
new Player() {ChampionId = 23}
}
});
matches.Add(matchFour);
return matches;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment