[英]How can I set custom property of an Entity type in LINQ to Entities query and still return a IQueryable<T>?
I have a EF4 model where I have Product entity that have one-to-many relation with Task entity (ChildTasks). 我有一个EF4模型,其中有与任务实体(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.
该项目有一个名为EstimatedProgress的自定义属性(不与任何列关联),在这里我必须存储有关项目的其他信息-每个子任务的计算加权平均进度。 The problem is that finally I can't return an IEnumerable
<Project>
but IQueryable <Project>
. 问题是,最终我不能返回IEnumerable
<Project>
而返回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. 问题是:是否可以用EF4 linq查询做类似的事情,或者您中的某些人有解决方法;)提前谢谢。
You can declare simple wrapper class 您可以声明简单的包装器类
public class ProductWithEstimation
{
public Product Product { get; set; }
public double? EstimatedTime { get; set; }
}
and return IQueryable<ProductWithEstimation>
instead. 并返回
IQueryable<ProductWithEstimation>
。
最简单的选择是将逻辑移到定制属性getter中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.