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…
This is the simplest thing I could come up with:
Please note that you might have to add some properties to your model, such as
Model.TimeSegmentandTimeSegment.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 == monthwill actually work with EF, but I think it will (with EF 4.0 that is).Update:
I’m not sure what you mean, but you can for instance filter the previous queryable like this:
Again, not sure what you exactly want, but if you want two separate lists that have no relation, you can do this:
Of course, if this is what you need, than it might be easier to rewrite the original query to something like this:
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!!!