I am a newbie in EF
and I think that sometimes it's easier to sacrifice the advantage of ORM. But I want to know if there is a way to execute this query using EF4
.
I have datawarehouse with fact table. Mapping looks like
public class GameResult
{
[Key]
public int GameResultId { get; set; }
public virtual Competition Competition { get; set; }
public virtual CustomDate DateGame { get; set; }
public virtual Contender ContenderFirst { get; set; }
public virtual Contender ContenderSecond { get; set; }
public virtual Location Location { get; set; }
public int ScoreContenderFirst { get; set; }
public int ScoreContenderSecond { get; set; }
public int PrizeStock { get; set; }
public int Budget { get; set; }
[NotMapped]
public int WinCount { get; set; }
[NotMapped]
public int FailCount { get; set; }
[NotMapped]
public int DeadHeatCount { get; set; }
[NotMapped]
public int CountGames { get; set; }
}
And query on sql
select
Contenders.Name,
sum((Case
when (ScoreContenderFirst > ScoreContenderSecond) then 1
else 0
end)) as wins,
sum ((Case
when (ScoreContenderFirst = ScoreContenderSecond) then 1
else 0
end)) as equals,
sum((Case
when (ScoreContenderFirst < ScoreContenderSecond) then 1
else 0
end)) as fail,
COUNT(GameResults.GameResultId)as countGames
from GameResults
inner join Contenders
on GameResults.ContenderSecond_ContenderId = Contenders.ContenderId
where GameResults.ContenderFirst_ContenderId = 42
group by Contenders.Name
UNION
select
Contenders.Name,
sum((Case
when (ScoreContenderFirst < ScoreContenderSecond) then 1
else 0
end)) as wins,
sum ((Case
when (ScoreContenderFirst = ScoreContenderSecond) then 1
else 0
end)) as equals,
sum((Case
when (ScoreContenderFirst > ScoreContenderSecond) then 1
else 0
end)) as fail,
COUNT(GameResults.GameResultId)as countGames
from GameResults
inner join Contenders
on GameResults.ContenderFirst_ContenderId = Contenders.ContenderId
where GameResults.ContenderSecond_ContenderId = 42
group by Contenders.Name
This sql
query means (I want to get game results of a particular team versus other teams (number of games that the particular team is winner, and number of games where the particular team is looser) )
_efContext.GameResult
.Where(game => game.ContenderFirst_ContenderId == 42)
.Select(game => new {
ContendersName = game.ContenderFirst.Name
, Win = game.ScoreContenderFirst > game.ScoreContenderSecond
, Draw = game.ScoreContenderFirst == game.ScoreContenderSecond
, Lose = game.ScoreContenderFirst < game.ScoreContenderSecond
})
.GroupBy(game => game.ContendersName)
.Select(grp => new {
ContendersName= grp.Key
, Wins = grp.Where(game => game.Win).Count()
, Draws = grp.Where(game => game.Draw).Count()
, Loses = grp.Where(game => game.Lose).Count()
})
Not include the countGames, since it is just the sum of wins, draws and loses.
For the second, you can just join with this query which looks like the first:
_efContext.GameResult
.Where(game => game.ContenderSecond_ContenderId == 42)
.Select(game => new {
ContendersName = game.ContenderSecond.Name
, Win = game.ScoreContenderFirst < game.ScoreContenderSecond
, Draw = game.ScoreContenderFirst == game.ScoreContenderSecond
, Lose = game.ScoreContenderFirst > game.ScoreContenderSecond
})
.GroupBy(game => game.ContendersName)
.Select(grp => new {
ContendersName= grp.Key
, Wins = grp.Where(game => game.Win).Count()
, Draws = grp.Where(game => game.Draw).Count()
, Loses = grp.Where(game => game.Lose).Count()
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.