navigate depth

Posts   
 
    
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 14-Oct-2014 00:49:48   

I have three related tables

List Task (ref ListID) TaskExecuted (ref TaskID)

in a linq query I'd like to check if any TaskExecuted records exists for a List.

I trying various variations of this:

var result = from l in metaDat.List select new { l.List, l.ListID, TaskCount = l.Task.Count(), AnyExecutedTasks = l.Task.TaskExecuted.Any() }

Is it at all possible to navigate this way?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Oct-2014 08:13:00   

In this case you can't navigate that way, because l.Task is a collection, so there is no l.Task.TaskExecuted navigator from a collection. You need a subquery. Something like:

var q = (from l in metaData.List
                select new
                {
                    ListId = l.ListId,
                    NumberOfTasks = l.Tasks.Count(),
                    HaveAnyExecutedTasks = l.Tasks
                                    .Where(t => t.TasksExecuted.Any())
                                    .Select(t => t.TaskId)
                                    .Any()
                });
David Elizondo | LLBLGen Support Team
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 14-Oct-2014 09:24:17   

daelmo wrote:

In this case you can't navigate that way, because l.Task is a collection, so there is no l.Task.TaskExecuted navigator from a collection. You need a subquery. Something like:

var q = (from l in metaData.List
                select new
                {
                    ListId = l.ListId,
                    NumberOfTasks = l.Tasks.Count(),
                    HaveAnyExecutedTasks = l.Tasks
                                    .Where(t => t.TasksExecuted.Any())
                                    .Select(t => t.TaskId)
                                    .Any()
                });

Thanks daelmo. Spot on...