Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 执行自联接时不返回数据_C#_Sql_Linq_Self Join - Fatal编程技术网

C# 执行自联接时不返回数据

C# 执行自联接时不返回数据,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

我有一张表格,里面有代理行的付款记录。我想把每家机构的付款总额分成两列,第一列是当天付款,第二列是付款前一天

所以我试着用这样的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.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());