Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 530701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:13:28+00:00 2026-05-13T09:13:28+00:00

Oh, I just find that the error is caused by another part of code.

  • 0

Oh, I just find that the error is caused by another part of code.
Case closed.

I have 2 tables

1- userinfo

id uid name 
1 11 Billy
2 22 Paul
3 33 Joshua

2- Score

 id uid score
 1  11  30
 2  22  40
 3  11  50
 4  11  60
 5  33  20
 6  33  70
 7  33  80

I have a class called ScoreUser

public class ScoreUser{
public long uid{get; set;}
public string name{get;set;}
public int score{get;set;}
}

I want to use linq to query the above two tables, get the maximum score of each user and map it into the ScoreUser Object.
I use the following code:

from s in Scores
join i in UserInfos
on s.uid equals i.uid
group uscore by new { s.uid, i.name} into g
let maxScore = g.Max(p => p.score)
select new ScoreUser
{
uid = g.Key.uid,
name = g.Key.name,
score = maxScore
 }

However, this code does does not work. It produces 7 objects instead of 3.
What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:13:28+00:00Added an answer on May 13, 2026 at 9:13 am

    You are also grouping by score when it should be the aggregator. Try this:

    from s in Scores
    join i in UserInfos on s.uid equals i.uid
    group by new { s.uid, i.name } into g
    select new ScoreUser
    {
       uid = g.Key.uid
       name = g.Key.name,
       score = g.Max(p => p.score)
    }
    

    (update)

    I see you found the problem. However I leave you here a test to this query:

        class UserInfo
        {
            public int Id { get; set; }
            public int UId { get; set; }
            public string Name { get; set; }
        }
    
        class Score
        {
            public int Id { get; set; }
            public int UId { get; set; }
            public int SScore { get; set; }
        }
    
        public class ScoreUser
        {
            public int uid { get; set; }
            public string name { get; set; }
            public int score { get; set; }
    
            public override string ToString()
            {
                return string.Format("UId:{0} Name:{1} Score:{2}", uid, name, score);
            }
        }
    
    
        static void Main(string[] args)
        {
    
            List<UserInfo> infos = new List<UserInfo>()
            {
                new UserInfo {Id = 1, UId = 11, Name = "Billy"},
                new UserInfo {Id = 2, UId = 22, Name = "Paul"},
                new UserInfo {Id = 3, UId = 33, Name = "Joshua"}
            };
    
            List<Score> scores = new List<Score>()
            {
                new Score {Id = 1, UId = 11, SScore = 30},
                new Score {Id = 2, UId = 22, SScore = 40},
                new Score {Id = 3, UId = 11, SScore = 50},
                new Score {Id = 4, UId = 11, SScore = 60},
                new Score {Id = 5, UId = 33, SScore = 20},
                new Score {Id = 6, UId = 33, SScore = 70},
                new Score {Id = 7, UId = 33, SScore = 80}
            };
    
            var qry = from s in scores
                      join i in infos on s.UId equals i.UId
                      group s by new { s.UId, i.Name } into g
                      select new ScoreUser
                      {
                          uid = g.Key.UId,
                          name = g.Key.Name,
                          score = g.Max(p => p.SScore)
                      };
    
            foreach (var su in qry)
            {
                Console.WriteLine(su);
            }
        }
    

    Prints:

    UId:11 Name:Billy Score:60
    UId:22 Name:Paul Score:40
    UId:33 Name:Joshua Score:80
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just spent half an one our to find out what caused the Error-Message
I just wanted to do some tutorials in WPF but I find that the
I was expecting to find that section within System.Web.Configuration namespace just like other Web.config
I am just starting with coding some JSP, which I find isn't all that
Is it just me who find the following code intrinsically backwards? I am using
I have just started using silverlight 2 beta and cannot find how to or
I'm writing some code to determine the name that an object is assigned to.
I've just installed CodeGear Studio 2009 and I didn`t find where I can put
I'm just starting to pick up ASP.Net MVC and find myself writing a lot
I'm just starting out with F# and I can't find the syntax to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.