C# 执行自联接时不返回数据
我有一张表格,里面有代理行的付款记录。我想把每家机构的付款总额分成两列,第一列是当天付款,第二列是付款前一天 所以我试着用这样的SQLC# 执行自联接时不返回数据,c#,sql,linq,self-join,C#,Sql,Linq,Self Join,我有一张表格,里面有代理行的付款记录。我想把每家机构的付款总额分成两列,第一列是当天付款,第二列是付款前一天 所以我试着用这样的SQL select p1.UserName, p1.PaymentAmount, p2.PaymentAmount from vw_Agency_Payment p1 join vw_Agency_Payment p2 on p1.UserName=p2.UserName where p1.PaymentDate = '2014-08-07' and p2.Pay
select p1.UserName, p1.PaymentAmount, p2.PaymentAmount
from vw_Agency_Payment p1
join vw_Agency_Payment p2 on p1.UserName=p2.UserName
where p1.PaymentDate = '2014-08-07'
and p2.PaymentDate = '2014-08-08'
它成功并返回数据
但当我将其转换为Linq时,如下所示:
var yesterday = DateTime.Today.AddDays(-1);
var tomorrow = DateTime.Today.AddDays(1);
var agencyPayment = from y in db2.vw_Agency_Payment
join t in db2.vw_Agency_Payment on y.UserName equals t.UserName
where y.PaymentDate >= yesterday
&& y.PaymentDate < DateTime.Today
&& t.PaymentDate >= DateTime.Today
&& t.PaymentDate < tomorrow
select new AgencyPaymentModel
{
agencyUserCode = y.UserName,
yesterdayPayment = y.PaymentAmount,
todayPayment = t.PaymentAmount,
growth = (t.PaymentAmount - y.PaymentAmount) / y.PaymentAmount * 100
};
return View(agencyPayment.OrderByDescending(c => c.growth).Take(100).ToList());
var昨天=DateTime.Today.AddDays(-1);
var明天=DateTime.Today.AddDays(1);
var agencyPayment=从db2.vw_Agency_Payment中的y开始
在db2.vw_代理中加入t,y.UserName等于t.UserName
其中y.PaymentDate>=昨天
&&y.PaymentDate<日期时间.今天
&&t.PaymentDate>=日期时间。今天
&&付款日期<明天
选择新的AgencyPaymentModel
{
agencyUserCode=y.UserName,
昨天付款=y.付款金额,
今天付款=t.付款金额,
增长=(t.PaymentAmount-y.PaymentAmount)/y.PaymentAmount*100
};
返回视图(agencyPayment.OrderByDescending(c=>c.growth).Take(100.ToList());
它不返回任何数据。
我不知道是什么错了 其中y.PaymentDate>=昨天
where y.PaymentDate >= yesterday
&& y.PaymentDate < DateTime.Today
&& t.PaymentDate >= DateTime.Today
&& t.PaymentDate < tomorrow
&&y.PaymentDate<日期时间.今天
&&t.PaymentDate>=日期时间。今天
&&付款日期<明天
没有结果将满足此条件:
从第1-2行开始,付款日期限制在昨天。。。与3号线相交将缩小为零
基本上你需要画一个合理的范围
另外,代码段2比代码段1包含更多的逻辑,您应该在相同的条件下测试它们
为什么不使用以下代码(使用datetime字段的日期部分)
Hi CodeFarmer,第1-2行的PaymentDate为Y,其余为T.var。昨天可能与Y.PaymentDate格式不同,有时由于格式不一致,日期比较将失败。其次,不要做(t.PaymentAmount-y.PaymentAmount)/y.PaymentAmount*100。如果您希望精度达到两倍或更高,Int值将向上取整。所有日期字段都是datetime。所有数字字段均为十进制。
var yesterday = DateTime.Today.AddDays(-1);
var agencyPayment = from y in db2.vw_Agency_Payment
join t in db2.vw_Agency_Payment on y.UserName equals t.UserName
where y.PaymentDate.Date = yesterday
&& t.PaymentDate.Date = DateTime.Today
select new AgencyPaymentModel
{
agencyUserCode = y.UserName,
yesterdayPayment = y.PaymentAmount,
todayPayment = t.PaymentAmount,
growth = (t.PaymentAmount - y.PaymentAmount) / y.PaymentAmount * 100
};
return View(agencyPayment.OrderByDescending(c => c.growth).Take(100).ToList());