2014-10-10 48 views
0

我在製作一個自動化數據消息的程序。在C中使用3個數組組織數據#

我有一個Excel工作表,它有一個名稱列,類型列和值列。

它看起來像這樣:

Name Type Value 
Sam A  32 
Ben B  65 
Sam B  213 
max B  23 
max C  24 
max C  12 
Ben C  45 

這個數據是不是真實的,但它是類似於我一起工作。

注意:一些名稱沒有某些類型。

我已經在3個數組arrName[]arrType[]加載的數據,並arrValue[]

我想使數據看起來像這樣與3D陣列arrPro[name, type, value]。所有相同類型的值應該屬於相同的名稱,並且所有值應該相加在一起以形成總值。

+0

加載數據表/數據集中的數據。如果您不知道如何將Excel轉移到DataSet,我可以爲您提供代碼。 – mybirthname 2014-10-10 08:37:45

+0

我已經將數據加載到3個不同的數組中。 我想要的數據看起來像 馬克斯 - A 144 - B 344 - Visual C 223 薩姆 - 一個343 - B 23 – Matty 2014-10-10 08:41:43

+0

好男人你的來電,祝你好運。可能有人會回答你。 – mybirthname 2014-10-10 08:52:46

回答

0

將excel數據直接載入到DataSet是最簡單的方法。

如果您確實需要3D陣列,我建議將 Tuple<string, string, int>作爲每行的數據保存器。

List<Tuple<string, string, int>>將保存您的所有數據。

0

這種結構可能保存數據

public class MyTypes 
    { 
     public string TypeName; 
     public List<NamesValues> Names; 
    } 


    public class NamesValues 
    { 
     public string Name; 
     public List<int> Values; 
    } 
0

你應該使用字典。

public Enum EType 
{ 
    A, 
    B, 
    C 
} 

public class PatientData 
{ 
    public EType Type { get; set; } 
    public int Value {get; set; } 
} 

public class PatientManager 
{ 
    private readonly Dictionary<String, List<PatientData>> _patients = new Dictionary<String, List<PatientData>>(); 


    public void AddPatientData(string name, EType type, int value) 
    { 
      var patientData = new PatientData 
       { 
        Type = type, 
        Value = value 
       }; 
      List<PatientData> patientDatas; 
      if (!dictionary.TryGetValue(name, out patientDatas)) 
      { 
       patientDatas = new List<PatientData>(); 
       dictionary.Add(key, patientDatas); 
      } 
      _patientDatas.Add(patientData); 
    } 

    public void LoadData(string[] names, EType[] types, int[] values) 
    { 
     var iMax = Math.Min(names.Length, Math.Min(type.Length, values.Length)); 
     for (var i = 0; i < iMax; i++) 
     { 
      AddPatientData(names[i], types[i], values[i]); 
     } 
    } 
} 
0

這不是simplier和清潔來存儲你的數據在某些List<YourObject>例如:

public class YourObject { 
    public string Name { get; set; } 
    public string Type{ get; set; } 
    public int Value { get; set; } 
} 

再算上你想使用LINQ(更小)的內容:

List<YourObject> yourObject = "your logic goes here (with whole data)"; 
List<YourObject> result = from s in yourObject 
    group s by new { s.Name, s. Type) into r 
    select new YourObject { Name = r.Name, Type = r.Type, Value = r.Sum(s => s.Value) }; 

我不確定這正是你想要的(分組類型)。