SQL> Linq to Sql, SQL Query works, Linq to SQL returns an empty dataset

I have this SQL query that works:

select sum(dbos.Points) as Points, dboseasons.Year 
    from dbo.StatLines dbos
    inner join dbo.Games dbog on dbog.GameId = dbos.GameId
    inner join dbo.Seasons dboseasons on dbog.Season_SeasonId = dboseasons.SeasonId
    where dbos.PlayerId = 3
    group by dboseasons.Year

It returns the points, year (56, 2016)

I am trying to convert this to a Linq query for use with EF.

I have

var query =
            from dbostats in _db.StatLines
            join dbogames in _db.Games on dbostats.GameId equals dbogames.GameId
            join dboseasons in _db.Seasons on dbogames.Season.SeasonId equals dboseasons.SeasonId
            where dbostats.PlayerId == player.PlayerId
            group dbostats.Points by dboseasons.Year into g
            select new
            {
                Year = g.Key,
                Points = g.Sum()
            };

        playerAndStatLines.StatLinesBySeason =
            query
            .ToList()
            .Select( r => new StatsBySeason
                {
                    Season = r.Year,
                    Points = r.Points
                });

Which returns an empty result set.

When I look at SQL generation, this is

SELECT 
[GroupBy1].[K1] AS [Year], 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    [Extent3].[Year] AS [K1], 
    SUM([Extent1].[Points]) AS [A1]
    FROM   [dbo].[StatLines] AS [Extent1]
    INNER JOIN [dbo].[Games] AS [Extent2] ON [Extent1].[GameId] = [Extent2].[GameId]
    INNER JOIN [dbo].[Seasons] AS [Extent3] ON [Extent2].[Season_SeasonId] = [Extent3].[SeasonId]
    WHERE ([Extent1].[Discriminator] IN (N'StatsBySeason',N'StatLines')) AND ([Extent1].[PlayerId] = 3)
    GROUP BY [Extent3].[Year]
)  AS [GroupBy1]

Which, as expected, returns an empty result set when executed against my DB.

The problem is this:

([Extent1].[Discriminator] IN (N'StatsBySeason',N'StatLines')) AND 

If I select this and run the generated query, I will return my result 2016, 56.

What is Extent1.Discriminator, why does it generate it from my linq query?

My MOdel classes:

public class PlayerAndStatLines
    {
        public PlayerWithTeam PlayerWithTeam { get; set; }
        public IEnumerable<StatsBySeason> StatLinesBySeason { get; set; }
    }


public class Season
    {
        public int SeasonId { get; set; }
        public int Year { get; set; }
    }


public class Game
    {
        public int GameId { get; set; }
        public int HomeTeamId { get; set; }
        public int AwayTeamId { get; set; }
        public int HomeScore { get; set; }
        public int AwayScore { get; set; }
        public DateTime DatePlayed { get; set; }
        public GameType GameType { get; set; }
        public int? PlayoffGameNumber { get; set; }
        public Season Season { get; set; }
    }

public class StatLines
    {
        public int StatLinesId { get; set; }
        public int GameId { get; set; }
        public int PlayerId { get; set; }
        public int TeamId { get; set; }
        public int Points { get; set; }
        public int DefensiveRebounds { get; set; }
        public int OffensiveRebounds { get; set; }
        public int Assists { get; set; }
        public int Turnovers { get; set; }
        public int Minutes { get; set; }
        public int Steals { get; set; }
        public int Blocks { get; set; }
        public int Fouls { get; set; }
        public int ThreePointFieldGoalsAttempted { get; set; }
        public int ThreePointFieldGoalsMade { get; set; }
        public int TwoPointFieldGoalsAttempted { get; set; }
        public int TwoPointFieldGoalsMade { get; set; }
        public int FreeThrowsMade { get; set; }
        public int FreeThrowsAttempted { get; set; }
        public bool Started { get; set; }
    }


public class StatsBySeason : StatLines
    {
        public int Season { get; set; }

        public string SeasonYears => Season + " / " + (Season + 1);
    }

If I run the following SQL:

select Discriminator from dbo.StatLines

I get 2 rows back, both empty.

Thank.

+4
1

(, ) . EF , TPH ( ) Discriminator, , , .

:

  • :

    public class StatsBySeason
    {
        public int Season { get; set; }
        public StatLines StatLines { get; set; }
    
        public string SeasonYears => Season + " / " + (Season + 1);
    }
    
  • EF StatsBySeason, :

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Ignore<StatsBySeason>();
        // ...
    }
    

    :

    [NotMapped]
    public class StatsBySeason : StatLines
    {
        // ...
    }
    
+2

Source: https://habr.com/ru/post/1666255/


All Articles