2017-10-12 108 views
0

我目前使用ASP.NET Core MVC爲我的應用程序,我不知道如何處理該問題。在另一個陣列中存儲多個陣列

說我有兩個陣列雙型

double[] questionOne = {1,4,5,2,4}; 
double[] questionTwo = {3,2,4,5,2}; 

的我想使用的方法將它們串聯在一起,並且將它們存儲在可能是一個字典,使得所存儲的值是一樣的東西

stud1 | 1,3 
stud2 | 4,2 
stud3 | 5,4 
stud4 | 2,5 
stud5 | 4,2 

因此我可以檢索這些值並計算每個學生的總值。


不知道會有多少問題。
我也不知道會有多少學生。
稍後我可以循環使用這些值,但現在它是一個固定值。

我應該將值存儲在字典,列表或元組中嗎?

此後,我該如何調用方法,以便返回值並顯示在「View」中?
我不需要將值放在表中,如果可能的話,一個簡單的原始輸出來檢查算法的想法。

+1

也許一個名字和元組的字典? – 2017-10-12 11:30:17

+0

就像'Dictionary >'? – David

回答

1

由於淨4.7您可以使用此代碼:

using System; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     double[] questionOne = {1, 4, 5, 2, 4}; 
     double[] questionTwo = {3, 2, 4, 5, 2}; 
     var combined = questionOne.Zip(questionTwo, (q1, q2) => (q1, q2)).ToList(); 
     Console.WriteLine(combined); 
    } 
} 
+0

Argl,對不起Sefe,我只是在你之後發佈... – schglurps

+0

感謝您的回覆!我沒有爲我的應用程序使用控制檯。你碰巧知道如何將它傳遞給我的觀點?我試過使用ViewData,但它不工作 – MaryLim

+0

請發佈您的代碼... – schglurps

0

您可以使用此結構:

Dictionary<string, string[]> myDictionary= new Dictionary<string, string[]>(); 

,那麼你只需要一個算法,該算法添加內容,如:

for(int i=0; i<array1.Length; i++) { 
    String[] data = new String[2]; 
    data[0] = array1[i]; 
    data[1] = array1[i]; 
    myDictionary.Add("student"+i, data); 
} 
1

你可以使用LINQ:

List<Tuple<double, double>> tuples = 
    questionOne.Zip(questionTwo, (one, two) => Tuple.Create(one, two)).ToList(); 

那結合了數組數組。你可以對學生做同樣的事情:

string[] students = new string[] {"stud1", "stud2", "stud3", "stud4", "stud5"}; 
Dictionary<string, Tuple<double, double>> result = students 
    .Zip(tuples, (student, tuple) => new { student, tuple }) 
    .ToDictionary(entry => entry.student, entry => entry.tuple); 

你可以看看結果here

+0

感謝您的回覆!如果我知道學生的數量和測試數量,我必須通過 – MaryLim