Reputation:
I'm writing a bit of code, and I'd like to play with doing it using the anonymous features of C#.
I'm writing a summary based on a DataTable returned from the SQL Server.
There are many ways I could write it already knowing Classical C# (???), but I'm interested in having a little fun.
So, here are the type of anonymous classes I want to have:
// Employee
var emp = new {
Badge = "000000",
Name = "No Name",
Parts = new List<Part>(),
Days = new List<DateTime>(),
};
// Part
var part = new {
SerialNumber = "N/A",
Date = DateTime.MinValue,
Badge = "000000",
};
Now, as I iterate over my DataTable entries, I want to sort my Parts by SerialNumber
.
The first thing I have to do is break the data down into days.
private void TestMethod(DateTime minDate, DateTime maxDate, DataTable table) {
int days = 1;
var nextDay = minDate.AddHours(24);
foreach (DataRow row in table.Rows) {
var dateTime = (DateTime)row["Date_Time"];
var emp = new {
Badge = row["Badge"].ToString(),
Parts = new List<Part>(),
Days = new List<DateTime>(),
};
var part = new {
SerialNumber = row["Serial_Number"].ToString(),
Date = dateTime,
Badge = row["Badge"].ToString(),
};
if (nextDay < dateTime) {
days++;
nextDay = nextDay.AddHours(24);
}
}
Now, it is getting a little interesting.
I need a way to store Part information for the different days and the different employees found for the period.
var parts = new List<typeof(part)>();
var emps = new List<typeof(emp)>();
Using typeof
(above) does not work!
What does?
Upvotes: 1
Views: 161
Reputation: 887433
You need to use type inference:
new[] { part }.ToList()
(you probably want to clear the list afterwards)
You can also make a helper method:
public static List<T> ListOf<T>(T sample) {
return new List<T>();
}
var parts = ListOf(part);
Upvotes: 6