1

我的数据库中有两个表,如下所示:

用户:

userid(int)
username(varchar)

分数:

scoreid(int)
result(bit)
attackinguserid(int) (same as User.userid)

它们还包含其他信息,但现在这并不重要。我想要的是获得 score = true 的前 25 名用户并像这样显示它们:

Username: Won:
Peter     28
Mike      25
Kim       20

等等......我一直在尝试的代码是这样的:

var winners = (from s in dc.Scores
                       from u in dc.Users
                       where (s.result) && (s.attackinguserid == u.userid)
                       group u by s
                       into groups
                       select new
                                  {
                                      Username= groups.Key,
                                      Won= groups.Count()
                                  }
                      ).OrderByDescending(x => x.Seire).Distinct().Take(25);

        gvBestPlayers.DataSource = winners; //gvBestPlayers = gridview
        gvBestPlayers.DataBind();

但这给了我在 DataBind() 上的错误。错误:NotSupportedException 未被用户代码处理。成员“System.Web.UI.Page.Title”不支持 SQL 转换。

4

2 回答 2

1

我自己解决了。这是工作代码:

var winners = (from s in dc.Scores 
               from u in dc.Users 
               where (s.result) && (s.attackinguserid == u.userid) 
               group s by u 
               into groups 
               select new 
               { 
                   Username= groups.Key.username, 
                   Won= groups.Count() 
               }).OrderByDescending(x => x.Seire).Distinct().Take(25).ToList(); 

gvBestPlayers.DataSource = winners; 
gvBestPlayers.DataBind();
于 2012-05-25T16:28:37.893 回答
0

试试这个..

var a = (from s in dc.Scores
                     join u in dc.Users on s.attackinguserid equals u.userid
                     let count = dc.Scores.Where(id => id.attackinguserid == u.userid ).Count()
                     where s.result == true
                     select new { Username = u.username, Won = count }).ToList().Distinct();
于 2012-05-23T09:23:01.560 回答