开发者

How can I set custom property of an Entity type in LINQ to Entities query and still return a IQueryable<T>?

开发者 https://www.devze.com 2023-01-07 12:43 出处:网络
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

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 projec开发者_开发知识库t 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.


You can declare simple wrapper class

public class ProductWithEstimation 
{ 
    public Product Product { get; set; } 
    public double? EstimatedTime { get; set; }
}

and return IQueryable<ProductWithEstimation> instead.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消