1

Haven't been able to specifically find, answers to my question..

I need to get a result that display: Project name ONCE, with all the users that have made timeEntries on that project, in a given date range.

And I need to get the timeEntries (hoursEntered) as well, so I can calculate how many hours have been spent on that project in total..

I am new to this, so I hope i have provided enough information..

my code:

var dateStart = new DateTime (2016,09,01);
var dateEnd   = new DateTime (2016,09,19);

var query = (from timeEntry in TimeEntries
        join task in Tasks on timeEntry.TaskID equals task.TaskID
        join project in Projects on task.ProjectID equals project.ProjectID
        where timeEntry.DateEntity >= dateStart && timeEntry.DateEntity <= dateEnd
        select new {
        User = timeEntry.User.FirstName + " "+timeEntry.User.LastName,
        Project = timeEntry.Task.Project.ProjectName,
        ProjectId = timeEntry.Task.ProjectID,
        HoursEntered = timeEntry.HoursEntered
        }).GroupBy (q => q.Project).Distinct();

i have 4 tables :

  • Projects (ProjectID, ProjectName)
  • Tasks (TaskID, ProjectID)
  • TimeEntries (HoursEntered, DateEntity, TaskID)
  • Users (FirstName, LastName)
andrelange91
  • 1,018
  • 3
  • 21
  • 48

2 Answers2

2
var query =
  from timeEntry in TimeEntries
  where timeEntry.DateEntity >= dateStart && timeEntry.DateEntity <= dateEnd
  select new {
    User = timeEntry.User.FirstName + " "+timeEntry.User.LastName,
    Project = timeEntry.Task.Project.ProjectName,
    ProjectId = timeEntry.Task.ProjectID,
    HoursEntered = timeEntry.HoursEntered
  }

var localRows = query.ToList();

var groups = localRows.GroupBy(x => x.Project);

var projects = groups.Select(g => new {
  Project = g.Key,
  Hours = g.Sum(x => x.HoursEntered),
  Users = g.Select(x => x.User).Distinct().ToList()
}).ToList();
Amy B
  • 108,202
  • 21
  • 135
  • 185
1

Maybe with:

var query = (from timeEntry in TimeEntries
        join task in Tasks on timeEntry.TaskID equals task.TaskID
        join project in Projects on task.ProjectID equals project.ProjectID
        where timeEntry.DateEntity >= dateStart && timeEntry.DateEntity <= dateEnd
        group timeEntry by new { proyect.ProjectID, proyect.ProjectName } into g
        select new {
        TimeEntries = g.ToArray(),
        Project = g.Key.ProjectName ,
        ProjectId = g.Key.ProjectID,
        HoursEntered = g.Sum(e => e.HoursEntered)
        });

I need more info to adjust it more, but I think this should help you. I did it on the browse, so mybe you need to check names.

Group by in LINQ

Community
  • 1
  • 1
  • Pretty good, have a +1. Also, be careful using this on a database. LinqToSql is likely to do extra queries to fetch the TimeEntries = g.ToArray part. – Amy B Sep 20 '16 at 07:02
  • this one gets way too much data that i don't need. and doesn't give me the usernames – andrelange91 Sep 20 '16 at 07:05
  • Well, as you are the only one who knows the data base and what you need, just adjust the query. +1 to the LinqToSql. But YAGNI except for a big Database. – Oscar Vicente Perez Sep 20 '16 at 07:06