I am working on an asp.net mvc3 application with linq2sql.
I have a List of SiteLog object type containing also for each object: A string named CLRExceptionType and a date named EntryDate. This List is included in a Dictionary:
private Dictionary<string, List<SiteLog>> dataBaseList = new Dictionary<string, List<SiteLog>>();
DataClasses1DataContext db = new DataClasses1DataContext("string1");
DataClasses1DataContext db2 = new DataClasses1DataContext("string2");
And I populate the vocabulary in the same controller in a function:
private void CreateDictionary()
{
dataBaseList.Add("db", db.SiteLogs.ToList());
dataBaseList.Add("db2", db2.SiteLogs.ToList());
}
So for each “Database” I have a list of Exception occurred.
Now I have to display on the view, for each database, the exception occurred grouped by exception and ordered by date.
So for example the output will be: Database1:
Exception1 (22 times) last occurred: 22.10.1989 19:30
Exception2 (2 times) last occurredd 20.5.1980 14.50
Then suddenly the object is updated with a new entry with the name Exception2 so I have to display:
Exception2 (3 times) last occurredd 20.7.2011 9:00
Exception1 (22 times) last occurred: 22.10.1989 19:30
How can I achieve this using the groupby or any other solution in the controller?
I return the dictionary to the view.
Thanks in advance
Try this:
This groups your dictionary by the key (= database). Each group is then grouped by the exception type. For each such group an anonymous type is selected with all the data you want to display. SelectMany flattens the data from an
IEnumerable<IEnumerable<T>>to anIEnumerable<T>. The result is then ordered by the database and then by the last occurred date.The result will be a list of anonymous type instance, each with the info you want to display.