views:

46

answers:

1

Hi, I have a EF4 model where I have Product entity that have one-to-many relation with Task entity (ChildTasks). I have to get some Projects from db based on some user criteria. The Project has one custom property (not associated with any column) named EstimatedProgress where I have to store additional information about Project - computed weighted average progress of each child task. The problem is that finally I can't return an IEnumerable<Project> but IQueryable<Project>. The code below demonstrates what I want to achieve (I know it does not compile).

var q = from p in ObjectContext.ProjectSet.Include("ProjectBase") 
        where p.ProjectBase.IsTemplate == false 
        from ct in p.ProjectBase.ChildTasks
        where ct.Progress != null
           && ct.EstimatedTime != null
        group ct by ct.prjProjectBase.Project into g
        select new {
            Project = g.Key,
            EstimatedProgress = g.Sum(oo => oo.Progress.Value * oo.EstimatedTime.Value) 
                              / g.Sum(oo => oo.EstimatedTime.Value),
        };

var sortedQ = q.OrderByDescending(o => o.Project.ProjectBase.Status)
          .ThenBy(o => o.Project.ProjectBase.Name);

// this is where I want to return IQueryable<Project> 
// where each project has set custom property EstimatedProgress
return sortedQ.Select(o =>
{
    o.Project.EstimatedTime = o.EstimatedProgress;
    return o.Project;
});

The question is: Is it possible to do something similiar with EF4 linq query or maybe some of you have a workaround ;) Thanks in advance.

A: 

The simplest option would be to move the logic into the custom property getter.

SLaks
Thanks for your answer SLaks. Unfortunately, that's not an option. Getter would be ok if I could use my IQueryable right after defining it (and it wouldn't be the best solution cause of poor efficiency - one DB call for every Project in the results). The reason I wrote about IQueryable is that I use Silverlight + WCF RIA Services and on the client side I want to use paging in datagrid (and only IQueryable on the server side fully supports it). Any other ideas?
wlf84k