2010-01-04 12 views
7

ओह, मुझे लगता है कि त्रुटि कोड के किसी अन्य भाग के कारण होती है। केस बंद हुआ।समूह में अधिकतम मान का चयन करने के लिए लिंक का उपयोग

2 टेबल

1-

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

2- UserInfo स्कोर

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

है मैं एक वर्ग ScoreUser

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

बुलाया मैं उपयोग करना चाहते हैं एल उपरोक्त दो तालिकाओं से पूछने के लिए inq, प्रत्येक उपयोगकर्ता का अधिकतम स्कोर प्राप्त करें और इसे स्कोरयूसर ऑब्जेक्ट में मैप करें। मैं निम्नलिखित कोड का उपयोग करें:

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 
} 

हालांकि, इस कोड काम नहीं करता है। यह 3. के बजाय 7 वस्तुओं का उत्पादन करता है मुझे क्या करना चाहिए?

+0

क्या वापस आ जा रहा है? क्या आपको कोई त्रुटि मिल रही है? – Theresa

+0

क्या आप स्कोर और उपयोगकर्ता इन्फोस टेबल को स्विच नहीं करना चाहिए? "मैं UserInfos में स्कोर से एस में शामिल हो जाता हूं ..." असल में, अभी आपका मुख्य चयन 7 रिकॉर्ड लंबा है। –

उत्तर

9

आप score द्वारा समूहबद्ध कर रहे हैं जब यह एग्रीगेटर होना चाहिए। इस प्रयास करें:

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

(अद्यतन)

मैं देख रहा हूँ आप समस्या का पता चला। हालांकि मैं तुम यहाँ इस क्वेरी के लिए एक परीक्षण छोड़:

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

प्रिंटों:

UId:11 Name:Billy Score:60 
UId:22 Name:Paul Score:40 
UId:33 Name:Joshua Score:80 
संबंधित मुद्दे