我已经创建了统计页面视图模型如下:的GroupBy复杂的查询
public class StatsSeasonViewModel
{
public int player_id { get; set; }
public string player_name { get; set; }
public int games_played { get; set; }
public int total_first { get; set; }
public int total_second { get; set; }
public int total_third { get; set; }
public int total_wickets { get; set; }
public double avg_wickets { get; set; }
public int total_points { get; set; }
public double avg_points { get; set; }
}
我有一个复杂的LINQ语句来填充模型。我觉得这可能是简单的,但我不知道如何做到这一点:
const int first_place = 5;
const int second_place = 3;
const int third_place = 1;
var model =
from s in _db.Stats
join p in _db.Players
on s.player_id equals p.player_id
where s.season_id == current_season
select new StatsSeasonViewModel
{
player_id = p.player_id,
player_name = p.name,
games_played = (from st1 in _db.Stats
where st1.player_id == s.player_id
select st1).Count(),
total_first = (from st2 in _db.Stats
where st2.player_id == s.player_id && st2.place == 1
select st2).Count(),
total_second = (from st3 in _db.Stats
where st3.player_id == s.player_id && st3.place == 2
select st3).Count(),
total_third = (from st4 in _db.Stats
where st4.player_id == s.player_id && st4.place == 3
select st4).Count(),
total_wickets = (from st5 in _db.Stats
where st5.player_id == s.player_id
select st5.wickets).Sum(),
avg_wickets = (from st5 in _db.Stats
where st5.player_id == s.player_id
select st5.wickets).Sum()/
(from st1 in _db.Stats
where st1.player_id == s.player_id
select st1).Count(),
total_points = (from st5 in _db.Stats
where st5.player_id == s.player_id
select st5.wickets).Sum() +
(
(from st2 in _db.Stats
where st2.player_id == s.player_id && st2.place == 1
select st2).Count()
) * first_place +
(
(from st3 in _db.Stats
where st3.player_id == s.player_id && st3.place == 2
select st3).Count()
) * second_place +
(
(from st4 in _db.Stats
where st4.player_id == s.player_id && st4.place == 3
select st4).Count()
) * third_place,
avg_points = (
(from st5 in _db.Stats
where st5.player_id == s.player_id
select st5.wickets).Sum() +
(
(from st2 in _db.Stats
where st2.player_id == s.player_id && st2.place == 1
select st2).Count()
) * first_place +
(
(from st3 in _db.Stats
where st3.player_id == s.player_id && st3.place == 2
select st3).Count()
) * second_place +
(
(from st4 in _db.Stats
where st4.player_id == s.player_id && st4.place == 3
select st4).Count()
) * third_place
)/
(from st1 in _db.Stats
where st1.player_id == s.player_id
select st1).Count()
};
所以我现在最大的问题是,我需要通过这个查询做一组,使其不会显示重复。但是,当我尝试添加组时,然后我迷失了如何在SELECT之后执行其余查询。我如何通过上面的查询来完成一个组,并获得我需要的结果?
编辑:FWIW这里是结果我得到:http://ecl.moyl.com/Home/Stats
第二个问题是当然的查询本身的复杂性。有没有更简单的方法来做到这一点?
中的值填充其统计创建1可枚举范围 - 3然后就过滤像这样 。这里(st => range.Contains(st.place)&& st.player_id = s.player_id)那么你需要按位分组。但在数据库中将所有这些视图都放在视图中可能更合理,因此您可以从中抓取。但你得到一个Upvote为你付出的努力, –
哈!感谢upvote!我想通过player_id分组,而不是放置。现在发生的事情是我得到所有球员的重复条目。我尝试将s.player_id添加到statGroup中,但是当我尝试构建模型时,select语句中所有复杂的东西都会弹出。您在数据库中创建视图可能是正确的 - 奇怪的是,我从来没有尝试过! – xgrinderx
您可能会发现该视图更有用,因为如果所有内容的索引都是正确的,则不需要执行尽可能多的工作来提取该查询。您可以将该视图拖入实体框架 –