2010-01-04 36 views
7

Ồ, tôi chỉ thấy rằng lỗi do một phần mã khác gây ra. Trường hợp đã đóng.Sử dụng LINQ để chọn giá trị tối đa trong một nhóm

Tôi có 2 bảng

1- UserInfo

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

2- Điểm

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

Tôi có một lớp được gọi là ScoreUser

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

Tôi muốn sử dụng l inq để truy vấn hai bảng trên, nhận được số điểm tối đa của mỗi người dùng và ánh xạ nó vào đối tượng ScoreUser. Tôi sử dụng mã sau:

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 
} 

Tuy nhiên, mã này không hoạt động. Nó tạo ra 7 đối tượng thay vì 3. Tôi nên làm gì?

+0

những gì đang được trả lại? bạn đang nhận được một lỗi? – Theresa

+0

Bạn không nên chuyển đổi các bảng Điểm và UserInfos? "từ i trong UserInfos tham gia s trong Điểm ..." Về cơ bản, ngay bây giờ lựa chọn chính của bạn là 7 hồ sơ dài. –

Trả lời

9

Bạn cũng đang nhóm theo score khi nó phải là tập hợp. Hãy thử điều này:

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) 
} 

(cập nhật)

Tôi thấy bạn tìm thấy vấn đề. Tuy nhiên tôi lại cho bạn ở đây một thử nghiệm để truy vấn này:

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 
Các vấn đề liên quan