Im trying to use LINQ to sort data that is going to be used into a report. The report strucuture looks like this:
Developer name
Date | Hours | Project Name | Week Ending
Date | Hours | Project Name | Week Ending
Date | Hours | Project Name | Week Ending
Developer name
Date | Hours | Project Name | Week Ending
Date | Hours | Project Name | Week Ending
Developer name
Date | Hours | Project Name | Week Ending
Date | Hours | Project Name | Week Ending
Date | Hours | Project Name | Week Ending
I am bringing all that data from a .dbml file. For such sorting i have this linq query:
DataClasses1DataContext context = new DataClasses1DataContext();
System.Data.Linq.ISingleResult<s_SummaryReportResult> res = context.s_timesheet_SummaryReport(startDate, endDate);
var orb = from c in res
group c by c.developer
into devgroup
select new
{
Dev = devgroup.Key,
devData = from o in res
select new {
o.Date,
o.Hours,
o.projectname,
o.week_ending
}};
IDictionary dic_orb = orb.ToDictionary(result => result.Dev.ToString(), result => result.devData);
foreach (KeyValuePair<string, object> item in dic_orb)
{
object brr = item.Value;
}
Sadly, on the foreach step i get an “InvalidCastException”.
Any clues in how i can resolve that foreach, or in a sense, how to group better that LINQ query?
Thanks in advance.
dic_orbis aDictionary<string, IEnumerable<'a>>where'ais the anonymous type defined in your query. The typeKeyValuePair<string, IEnumerable<'a>>is not convertible toKeyValuePair<string, object>hence the invalid cast. The fix is to declareitemusing var in yourforeachloop:Since
item.Valueis anIEnumerable<'a>you can iterate it usingIf you just want to iterate all nested collections you can use