首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >左外连接IEnumerable<T>到DataTable参数不能为空错误。

左外连接IEnumerable<T>到DataTable参数不能为空错误。
EN

Stack Overflow用户
提问于 2015-04-02 19:04:48
回答 2查看 157关注 0票数 0

我有这个:

代码语言:javascript
复制
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中如下所示:

代码语言:javascript
复制
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应该处理好这个问题。我遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-02 20:07:20

弄明白了。localnull的身份出现,我仍然需要一种将其与server.reportDate进行比较的方法。我用了一个三元操作符。而且,由于我将DateTime.MinValue指定为默认日期,所以能够删除|| local == null

代码语言:javascript
复制
where DateTime.Compare(server.reportDate, (local != null) ? (DateTime)local["reportDate"] : DateTime.MinValue) > 0
票数 1
EN

Stack Overflow用户

发布于 2015-04-02 20:25:16

当在linq中对对象执行左联接时,您应该更喜欢在空时提供默认值。那么您的查询就不需要做太多更改了。

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29420552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档