I have an asp.net web application and I want to export to a PDF tables in a particular way.
Tables will have data of type Test. tests have a type(by type i mean an int to tell them apart) and for every type i'm creating a TestTable and for every Test a column in each TestTable , there are 9 types. That's why there are 9 TestTables but in tests there might be for example only two different types.
Is there a better way to do this without creating two arrays that their might be useless
public IEnumerable<TestTable> TestHistoryItems(GetTestExportRequest request)
{
IEnumerable<Test> tests = repository.FindBy(t => t.Visit.pid == request.PetId &&
t.Visit.dateofvisit >= request.Start && t.Visit.dateofvisit <= request.End).OrderByDescending(t => t.stamp);
TestTable[] testTables = new TestTable[9] // all the TestTables are 9 but it might need less depending on tests
{ //every TestTable needs tests, of type IEnumerable<Test>.
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable(),
new TestTable()
};
List<Test>[] testTypes = new List<Test>[9]
{
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>(),
new List<Test>()
};
foreach (Test test in tests)
{
switch (test.type)
{
case 3: GetTable(test, ref testTables[0], 3); testTypes[0].Add(test); break;
case 4: GetTable(test, ref testTables[1], 4); testTypes[1].Add(test); break;
case 5: GetTable(test, ref testTables[2], 5); testTypes[2].Add(test); break;
case 6: GetTable(test, ref testTables[3], 6); testTypes[3].Add(test); break;
case 7: GetTable(test, ref testTables[4], 7); testTypes[4].Add(test); break;
case 8: GetTable(test, ref testTables[5], 8); testTypes[5].Add(test); break;
case 9: GetTable(test, ref testTables[6], 9); testTypes[6].Add(test); break;
case 10: GetTable(test, ref testTables[7], 10); testTypes[7].Add(test); break;
case 16: GetTable(test, ref testTables[8], 16); testTypes[8].Add(test); break;
}
}
int i = 0;
foreach (TestTable test in testTables)
{
testTables[i].Tests = testTypes[i]; //add tests in testTables
i++;
}
return testTables;
}
public void GetTable(Test test, ref TestTable table, int index)
{
if (table.Type != index)
{
table.TestName = test.TestTypeName; //One Time for every TestTable
table.Type = index; //One Time for every TestTable
table.Rows = test.TestDatas.Count(); //One Time for every TestTable; Rows of the table
}
table.Columns++; // columns are the number of tests in every TestTable
}
Here is the Class TestTable.
public class TestTable
{
public string TestName { get; set; }
public int Type { get; set; }
public IEnumerable<Test> Tests { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
}
The program works as expected but I think this is wrong. Can't think of a better way to do this, I'm new to coding.
How can I make this code better?