2014-05-05 38 views
-3

我有滿屋子的學生和我已經跟蹤他們做了哪些活動,他們在使用該代碼的每一項活動取得了什麼成績:如何重新組織我的C#列表,使其圍繞不同的變量?

public class TrackActivity 
{ 
    public String StudentName {get; set;} 
    public String ActivityName {get; set;} 
    public int ActivityScore {get; set;} // out of 10 points 
} 

我公司目前擁有的「TrackActivity」列表,並希望重新組織它,以便它有一個以學生姓名爲中心的組織。也就是說,我想通過StudentName來組織我的數據,然後從那裏找出他們參與的ActivityName以及他們的ActivityScore是什麼。

比如,我從我的TrackActivity的名單知道:

Barbara, juggling, 9 
Barbara, cycling, 7 
Chris, cycling, 9 
Dennis, juggling, 8 
Dennis, cycling, 6 
Dennis, archery, 10 

我想整理我有什麼讓我知道的信息,而不是這樣:

Barbara, 2 activities: juggling (9), cycling (7) 
Chris, 1 activity: cycling (9) 
Dennis, 3 activities: juggling (8), cycling (6), archery (10) 

我知道如何將我的列表輸出到文本文件進行查看。如何輸出我的列表,使其格式化爲後者?

+5

Errrr,你有什麼試過?你有沒有可能實際使用這種重組數據結構的代碼? (這將指出結構的外觀。) 因爲這似乎不是一個完整的問題。 – catfood

+0

我想我的問題是這樣的:如何格式化我當前的輸出列表,以便我可以在文本文件中以我想要的方式查看數據?我知道如何輸出到文本,但我不知道如何重組我的內容。 – phan

回答

3
List<TrackActivity> activities = new List<TrackActivity> { 
    new TrackActivity { StudentName = "Barbara", ActivityName = "juggling", ActivityScore = 9 }, 
    new TrackActivity { StudentName = "Barbara", ActivityName = "cycling", ActivityScore = 7 }, 
    new TrackActivity { StudentName = "Chris", ActivityName = "cycling ", ActivityScore = 9 }, 
    new TrackActivity { StudentName = "Dennis", ActivityName = "juggling ", ActivityScore = 8 }, 
    new TrackActivity { StudentName = "Dennis", ActivityName = "cycling ", ActivityScore = 6 }, 
    new TrackActivity { StudentName = "Dennis", ActivityName = "archery ", ActivityScore = 10 }, 
}; 
foreach (var student in activities.GroupBy(a => a.StudentName)) 
{ 
    Console.WriteLine("{0}, {1} {2}: {3}", 
         student.Key, 
         student.Count(), 
         student.Count() > 1 ? "activities" : "activity", 
         string.Join(", ", student.Select(a => string.Format("{0} ({1})", a.ActivityName, a.ActivityScore)).ToArray())); 
} 
+0

我得到的錯誤是:無法從'System.Collections.Generic.IEnumerable '轉換爲'string []'。錯誤'string.Join(string,string [])'的最佳重載方法匹配有一些無效參數 – phan

+1

@phan您是否使用.Net 2.0/3.5?我在代碼中添加了'.ToArray()'。 –

+0

是的,謝謝,添加.ToArray()解決了我的問題! – phan

0

改變你List<TrackActivity>Dictionary<string, TrackActivity> 然後創建一個從Dictionary<string, TrackActivity>

public class TrackActivitys: dictionary<string, TrackActivity> 
    { 
     public void Add(string studentName, string activityName, int activityScore) 
     { 
      base.Add(studentName, 
        new TrackActivity(studentName, activityName, activityScore)); 
     } 
     public TrackActivity this[string studentName] 
     { 
      get { return base[studentName]; } 
     } 
    } 
0

我會建議重新組織這樣的代碼派生的集合類:

// Since your students will use very typical activities, why not have a separate class just for activity type? 
public class Activity 
{ 
    // Immutability is optional — I just tend to specify it by default. 
    public readonly string Name; 

    public Activity(string _Name) 
    { 
     this.Name = _Name; 
    } 
} 

public class Student 
{ 
    public readonly string Name; 

    // It probably shouldn't be exposed like this 
    public Dictionary<Activity, int> Scores = new Dictionary<Activity, int>(); 

    public Student(string _Name) 
    { 
     this.Name = _Name; 
    } 

    public string Report() 
    { 
     string result = Name; 
     if (Scores != null && Scores.Count > 0) 
     { 
      result += ", " + Scores.Count + " activities: "; 
      result += string.Join(", ", Scores.Select(s => c.Key.Name + "(" + c.Value + ")")); 
     } 
     return result; 
    } 

} 

我不現在不需要C#編譯器,所以這可能會有一兩個小錯誤。

0

好像你需要兩個班,學生(誰擁有活動列表):

public class Student 
{ 
    public String Name { get; set; } 
    public List<Activity> Activities { get; set; } 
} 

和保持信息的活動類:

public class Activity 
{ 
    public String Name {get; set;} 
    public int Score {get; set;} // out of 10 points 

    public override string ToString() 
    { 
     return String.Format("{0} ({1})", Name, Score); 
    } 
} 

輸出會是這樣像:

foreach (Student s in students) 
    Console.Writeline("{0}, {1} activities: {2}", s.Name, s.Activities.Count, String.Join(',', s.Activities.Select(a => a.ToString()).ToArray()); 

你也可以「活動」分隔成自己的類(獨立於SCOR e)按照其他答案中的建議,使用字典跟蹤它們。

+0

這樣,您就有很多重複名稱的活動對象,例如,您將無法輕鬆地將「PE」重命名爲「體育」。 –

+0

@golergka,你絕對正確。我相信OP的代碼也會有這個問題。你可以通過3個類來解決這個問題,我會編輯以包含這種可能性。 – BradleyDotNET

+0

@BradleyDotNET,同意重命名問題無論如何都是該方法特有的 –