我正在寻找一些帮助转换以下sql查询到LINQ to entities in C#。
SELECT f.FundId, u.UnitValue
FROM Fund f
INNER JOIN FundUnit u ON f.FundId= u.FundId
WHERE u.EffectiveDate = (SELECT MAX(effectivedate)
FROM FundUnit k
WHERE u.FundId = k.FundId)
AND f.Active = 1
ORDER BY f.FundId该查询读取数据库中的所有活动基金及其最新单位值。基金表,其中包含每个基金的记录。FundUnit表包含每个基金每个日期的单位价值记录。以前日期的单位值也保留在表中,以维护历史记录。
并非所有基金每天都会收到新的单位价值,因此所有基金的最新生效日期不一定相同。因此,需要对单位表应用max(有效)函数,需要对每个基金应用max(有效值)函数-因此需要相关子查询。
谢谢
发布于 2010-11-23 01:30:01
为了回答这个问题,我必须假设您的实体的名称。
SELECT f.FundId,u.UnitValue FROM f INNER JOIN FundUnit u ON f.FundId= u.FundId WHERE u.EffectiveDate = (SELECT MAX(有效) FROM FundUnit k WHERE u.FundId = k.FundId) AND f.Active =1 ORDER BY f.FundId
我将假设一个基金实体和一个FundUnit实体,就像这个例子一样。
var query = from f in Fund
from u in FundUnit
where f.FundId == u.FundId
&& u.EffectiveDate == (from k in FundUnit
where u.FundId = k.FundId
select EffectiveDate).Max()
&& f.Active == 1
select new { Id = f.FundId, EffectiveDate = u.EffectiveDate } // Add any fields you need
order by f.FundId这一切都是我凭记忆做的,没有进行测试,可能会有一些小问题。如果我有时间,我会测试它。
发布于 2010-11-23 01:23:28
我认为这应该会有帮助:directly execute sql strings
https://stackoverflow.com/questions/4248193
复制相似问题