1

I have two tables in my database who looks like this:

User:

userid(int)
username(varchar)

Score:

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

They also contain other information but that's not important right now. What i want is to get the top 25 users who has score = true and display them like this:

Username: Won:
Peter     28
Mike      25
Kim       20

And so on... The code I have been trying is this:

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();

But this gives me error on the DataBind(). The error: NotSupportedException was unhandled by user code. The member 'System.Web.UI.Page.Title' has no supported translation to SQL.

9
  • 2
    Does the query work when you put the results in a List<T> instead of databinding it?
    – Myrtle
    May 23, 2012 at 7:38
  • Im not sure what you mean @Aphelion or how to put the results in a list<T>.
    – Twistar
    May 23, 2012 at 7:41
  • That does not work @Aphelion. I get the same error.
    – Twistar
    May 23, 2012 at 7:45
  • Is it somehow possible that the query accesses the System.Web.UI.Page.Title property? The query tries to translate a part that it does not support as the error indicates. You need to find that part and exclude it from the query.
    – Myrtle
    May 23, 2012 at 7:50
  • You don't have any idea what i can be? have been trying and failing with almost every part of the query now.
    – Twistar
    May 23, 2012 at 7:59

2 Answers 2

1

Solved it my self. Here is the working code:

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();
0

try this..

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.