我有这个:
var initialRelease = from c in campaignAvailability
where c.reportStatus == "Initial Release"
select c;
var results = from server in initialRelease
join local in table.AsEnumerable()
on server.campaignId equals local.Field<long>("campaignId") into ls
from local in ls.DefaultIfEmpty()
where DateTime.Compare(server.reportDate, local.Field<DateTime>("reportDate")) > 0 || local == null
select server;
//add it to list of campaigns to process
results.Select(m => new { m.campaignId, m.reportDate }).Distinct()
.Select(n => new CampaignReportDate() {
campaignId = n.campaignId,
reportDate = n.reportDate
}).ToList().ForEach(c => campaignsToProcess.Add(c));我希望在SQL中如下所示:
SELECT a
FROM ienumerable AS a
LEFT OUTER JOIN table AS b
ON
a.id = b.id
WHERE b.some_date > a.some_date
OR b IS NULL我知道where linq子句不能比较null日期。但是,据我所知,|| local == null应该处理好这个问题。我遗漏了什么?
发布于 2015-04-02 20:07:20
弄明白了。local以null的身份出现,我仍然需要一种将其与server.reportDate进行比较的方法。我用了一个三元操作符。而且,由于我将DateTime.MinValue指定为默认日期,所以能够删除|| local == null。
where DateTime.Compare(server.reportDate, (local != null) ? (DateTime)local["reportDate"] : DateTime.MinValue) > 0发布于 2015-04-02 20:25:16
当在linq中对对象执行左联接时,您应该更喜欢在空时提供默认值。那么您的查询就不需要做太多更改了。
var results =
from server in initialRelease
join local in table.AsEnumerable()
on server.campaignId equals local.Field<long>("campaignId") into ls
from local in ls.DefaultIfEmpty(table.NewRow())
where DateTime.Compare(server.reportDate,
local.Field<DateTime?>("reportDate") ?? Datetime.MaxValue) > 0
select server;https://stackoverflow.com/questions/29420552
复制相似问题