问题
I hope I am wording my question the right way, and using the correct terminology. I have an array of student names, arrSummary, that is distinct (no duplicates).
Then, I have another array, arrTest1, that contains a list of the same students' names and their test scores on Test1. Then I have a third array, arrTest2, that contains a list of their names and their test scores on Test2.
arrTest1 and arrTest2 are such that arrTest1[0].Name = "Bob" and arrTest1[0].Score = "98".
How do I make the array, arrSummary, such that arrSummary[0].Name = "Bob", arrSummary[0].Score1 = "98", arrSummary[0].Score2 = "76"?
There are different types in the array (string, integer, integer).
回答1:
var arrSummary = arrTest1.Join(arrTest2, person => person.Name, person => person.Name
, (first, second) => new
{
Name = first.Name,
Score1 = first.Score,
Score2 = second.Score,
})
.ToArray();
Note that this is querying the first two arrays and creating a brand new array. Here I used an anonymous type for the query result; if the query results will be used outside of this method you'll want to make a new class with those 3 properties and project the results to instances of that type (just put that type after new).
You can't change the dimensions, the type, or even the size of an array once it is created. If the summary array already is of a type that has all three properties then you could in theory go through and populate it, but it would probably be easier to just generate the summary array from scratch, which is why that's what I've done here.
回答2:
Use a single list or array that stores data as fields in a class rather than three different arrays, like so:
class TestResult {
public String Name;
public Int32 TestResult1;
public Int32 TestResult2;
}
TestResult[] testResults = new TestResult[ numberOfStudents ];
testResults[0].Name = "Mr. Foo bar";
testResults[0].TestResult1 = 65;
testResults[0].TestResult2 = 76;
回答3:
You need to create a type and use that in your array:
public class TestSummary {
public string StudentName {get;set;}
public int Score1 {get;set;}
public int Score2 {get;set;}
}
OR
You could do this the RIGHT way:
public class Score{
public int TestId {get;set;}
public int StudentId {get;set;}
public int Score {get;set;}
}
public class Student{
public int ID {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public List<Score> Scores;
}
Then you should have a List<Student> instead of your summary array thing.
回答4:
var scores1 = arrTest1.ToDictionary(score => score.Name);
var scores2 = arrTest2.ToDictionary(score => score.Name);
var output = arrSummary.Select(name=>new {Name=name, Test1 = scores1[name], Test2 = scores2[name]).ToArray();
(Note this only works if your arrTest arrays don't have any missing entries. i.e., there's no names in arrSummary that aren't in arrTest1 or arrTest2.)
回答5:
Create a class or struct that contains the data you want to store and create an array of those objects! Better yet you may consider using a List!
public class Student
{
public string name {get;set;}
public int score1 {get;set;}
public int score2 {get;set;}
}
...
List <Student> = new List<Student>();
List.Add({name="Len", score1=90, score2=100});
来源:https://stackoverflow.com/questions/12321842/how-do-i-add-more-elements-or-dimensions-to-a-single-dimension-array