Question

I have these classes below

public class Match
{
    public int Id { get; set; }

    public MatchType Type { get; set; }

    public MatchStatus Status { get; set; }

    public LeagueGroup LeagueGroup { get; set; }

    public Tournament Tournament { get; set; }

    public IList<Score> Scores { get; set; }

    public IList<Team> Teams { get; set; }
}

public class Score
{
    public int Id { get; set; }

    public Team Team { get; set; }

    public Nullable<int> Points { get; set; }

    public Match Match { get; set; }
}

public class Team
{
    public int Id { get; set; }

    public IList<Competitor> Competitors { get; set; }

    public IList<Match> Matches { get; set; }

    public Team()
    {
        Competitors = new List<Competitor>();
        Matches = new List<Match>();
    }
}

public class Competitor
{
    public int Id { get; set; }

    public string CompetitorName { get; set; }

    public int UserId { get; set; }

    public DateTime SignUpDate { get; set; }
}

So there is a Match, Which has a list of Scores (usually two), these score have a team attached to them and then each team has a list of competitors (usually 1- 2).

What I am looking to do is find the score (Points) for a Match for a certain competitor by UserId.

All of the matches will have had the user play in them so there is no need to worry about not finding a score for them.

I have tried lot of different combinations and am currently sat on :

match.Scores.FirstOrDefault(x => x.Team.Competitors.FirstOrDefault(u => u.UserId == User.UserId)).Points.Value;

I think I am going the wrong way around it though, going outwards in when i should be doing it the other way.

Any Help would be greatly appreciated

Thank you.

Was it helpful?

Solution

Found the answer.

match.Scores.FirstOrDefault(x => x.Team.Competitors.Any(u => u.UserId == User.UserId)).Points.Value;

So this basically says.

Get me the points property for the first score.

that has a team that contains a competitor that userId is equal to the User objects Id

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top