1 表 History with Date (date) 1 table AccountCategories with Account (nvarchar(50)) 1 table Balance with Account(nvarchar(50)), Date(datetime), Balance(float)
我需要一个 LINQ 或 SQL Server 查询来从 Balance 中获取以下信息:
(日期,帐户,余额)历史记录中的每个日期,AccountInfo 中的每个帐户,该日期下午 4:10:00 之前的最后一条记录的余额
下面的我的 SQL 返回下午 4:10:00 之前的所有记录,但我只想要每个帐户的最后一个:
select [Date],Account,Balance from Balance where Account in (select Account from AccountCategories) and DATEADD(dd, 0, DATEDIFF(dd, 0, [Date])) in (select DATEADD(dd, 0, DATEDIFF(dd, 0, [Date])) from History) and [Date] <= DATEADD(mi,970,DATEADD(dd, DATEDIFF(dd, 0, [Date]), 0)) order by [Date] desc,Account
这个 LINQ 也不能完全得到我想要的,因为 LINQ to Entities 不支持指定类型成员 'Date'。仅支持初始化程序、实体成员和实体导航属性。':
TimeSpan ts = new TimeSpan(16, 10, 0);
var accts = AccountCategories.Select(u => u.Account);
var pastDates = Histories.Select(u => u.Date);
var dailyBalance = (from dailyBal in Balances where accts.Contains(dailyBal.Account) group dailyBal by dailyBal.Account into a select a.OrderByDescending(t => t.Date).Where(u => u.Date <= (u.Date.Date + ts)).FirstOrDefault());
有任何想法吗?