我有一个 EF4 模型,其中我有与任务实体 (ChildTasks) 具有一对多关系的产品实体。我必须根据某些用户标准从数据库中获取一些项目。该项目有一个名为 EstimatedProgress 的自定义属性(不与任何列关联),我必须在其中存储有关项目的其他信息 - 每个子任务的计算加权平均进度。问题是我最终无法返回 IEnumerable <Project>
但是 IQueryable <Project>
强>。下面的代码演示了我想要实现的目标(我知道它无法编译)。
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;
});
问题是:是否可以使用 EF4 linq 查询做一些类似的事情,或者你们中的一些人可能有解决方法;)提前致谢。
最佳答案
你可以声明简单的包装类
public class ProductWithEstimation
{
public Product Product { get; set; }
public double? EstimatedTime { get; set; }
}
并返回IQueryable<ProductWithEstimation>
相反。
关于c# - 如何在 LINQ to Entities 查询中设置实体类型的自定义属性并仍然返回 IQueryable<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3231766/