Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3611944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:56:16+00:00 2026-05-18T21:56:16+00:00

I’m hoping this will be a rather simple question for anyone who’s good at

  • 0

I’m hoping this will be a rather simple question for anyone who’s good at Linq. I’m struggling to come up with the right Linq expression for the following. I’m able to hack something to get the results, but I’m sure there’s a proper and simple Linq way to do it, I’m just not good enough at Linq yet…

I have a database accessed through Entity Framework. It has a number of Tasks. Each Task has a collection of TimeSegments. The TimeSegments have Date and Employee properties.

What I want is to be able to get the tasks for a certain employee and a certain month and the timesegments for each task for that same month and employee.

Again, the tasks do not in themselves have month nor date information, but they do by the TimeSegments associated with each task.

Very simplified it looks sort of like this:

    public class Model //Simplified representation of the Entity Framework model
    {
        public List<Task> Tasks { get; set; }
    }

    public class Task
    {
        public int Id { get; set; }
        public List<TimeSegment> TimeSegments { get; set; }
        public Customer Customer { get; set; }
    }

    public class TimeSegment
    {
        public int Id { get; set; }
        public string Date { get; set; }
        public Employee Employee { get; set; }
    }

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

So how do I do this as simply as possible with Linq? I.e. tasks and associated timesegments for a certain month and employee. I would also like to be able to get it by Customer BTW…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:56:17+00:00Added an answer on May 18, 2026 at 9:56 pm

    This is the simplest thing I could come up with:

    var tasksWithSegments =
        from segment in model.TimeSegments
        where segment.Date.Month == month
        where segment.Employee.Id == employeeId
        group segment by segment.Task into result
        select new
        {
            Task = result.Key,
            TimeSegments = result.ToArray()
        };
    

    Please note that you might have to add some properties to your model, such as Model.TimeSegment and TimeSegment.Task.

    The trick with LINQ queries often is to start at the right collection. In this case the ideal starting point is TimeSegments.

    ps. I’m not sure whether Date.Month == month will actually work with EF, but I think it will (with EF 4.0 that is).


    Update:

    Could you show how to extend this
    query and get the tasks for a
    particular Customer as well?

    I’m not sure what you mean, but you can for instance filter the previous queryable like this:

    var tasksWithSegmentsForCustomers =
        from taskWithSegments in tasksWithSegments
        where taskWithSegments.Task.Customer.Id == customerId
        select taskWithSegments;
    

    Can I get the return type to be a list
    of Tasks with a list of TimeSegments
    if I have this in a method?

    Again, not sure what you exactly want, but if you want two separate lists that have no relation, you can do this:

    List<Task> tasks = (
        from taskWithSegments in tasksWithSegments
        select taskWithSegments.Task).ToList();
    
    List<TimeSegments> segments = (
        from taskWithSegments in tasksWithSegments
        from segment in taskWithSegments.Segments
        select segment).ToList();
    

    Of course, if this is what you need, than it might be easier to rewrite the original query to something like this:

    List<TimeSegment> segments = (
        from segment in model.TimeSegments
        where segment.Date.Month == month
        where segment.Employee.Id == employeeId
        select segment).ToList();
    
    List<Task> allTasks =
        segments.Select(s => s.Task).Distinct().ToList();
    

    Once you got the hang of writing LINQ queries, there is no way you want to go back to writing SQL statements or old-fashion foreach statements.

    Think LINQ!!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.