I have written a LINQ query which returns a new type of class, becuse I have used GROUP BY. Therefore, I have created a new class to cast Anonymous type to Strong Type. Here is my LINQ...
var result = rpt
.GroupBy(x => new
{
CreateTime = EntityFunctions.TruncateTime(x.CreatedTime),
x.UserId,
x.ProjectId
})
.Select(x => new
{
UserId = x.Key.UserId,
ProjectId = x.Key.ProjectId,
CreateTime = x.Key.CreateTime,
TotalMinutesSpent = x.Sum(z => z.MinutesSpent)
})
.OfType<List<UserTransactionReport>>().ToList();
My newly created class is like below...
public class UserTransactionReport
{
public int UserId { get; set; }
public int ProjectId { get; set; }
public DateTime CreateTime { get; set; }
public int TotalMinutesSpent { get; set; }
}
How can I convert this anonymous to strong?
You can create strongly typed objects in the select:
List<UserTransactionReport> result =
...
.Select(x => new UserTransactionReport
{
UserId = x.Key.UserId,
ProjectId = x.Key.ProjectId,
CreateTime = x.Key.CreateTime,
TotalMinutesSpent = x.Sum(z => z.MinutesSpent)
}).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With