【问题标题】:Remove records from one datatable based on records from another datable根据另一个数据表中的记录从一个数据表中删除记录
【发布时间】:2014-09-22 13:47:53
【问题描述】:

我在第一个数据表中有以下结果:

上传文件的数据表

clientcode    jobcode    joburl                          DocumentLink
1             1          http://ourlocaldomain.com       http://ourlocaldomain.com/documents/1.pdf
1             2          http://ourlocaldomain.com       http://ourlocaldomain.com/documents/2.pdf

包含所有客户端和作业的数据表,无论文档是否已上传。 (第二个数据表上不存在文档链接列)

clientcode    jobcode    joburl                          
1             1          http://ourlocaldomain.com       
1             2          http://ourlocaldomain.com       
1             3          http://ourlocaldomain.com    
1             4          http://ourlocaldomain.com

我的第三个数据表应返回 Datatable2 中不在数据表 1 中的所有记录,基于与客户端代码和作业代码匹配的记录。

我想这应该能够以某种方式使用 LINQ,但不知道从哪里开始挖掘:

我的代码是:

var keywordQueryAllInfoLists = new KeywordQuery(site);
dataTableAllInfoLists = KQLUtilities.ExecuteKql(keywordQueryAllInfoLists, queryTextAllInfoLists, selectedProperties.ToArray(), keywordQueryAllInfoLists.SortList);

var keywordQuery = new KeywordQuery(site);
dataTableAllEngagementLetters = KQLUtilities.ExecuteKql(keywordQuery, queryTextAllLetterOfEngagement, selectedProperties.ToArray(), keywordQuery.SortList);

编辑 我尝试了以下方法:

resultingDatatable = dataTableAllEngagementLetters.Select()
                    .Where(x => !dataTableAllInfoLists.Select(string.Format("ClientCode = {0} and JobCode = {1}", x["ClientCode"], x["JobCode"]))
                    .Any())
                    .CopyToDataTable();


resultingDatatable = dataTableAllEngagementLetters.Select()
                    .Where(x => !dataTableAllInfoLists.Select(string.Format("ClientCode = [{0}] and JobCode = [{1}]", x["ClientCode"], x["JobCode"]))
                    .Any())
                    .CopyToDataTable();

这两个给我抛出了以下异常:http://screencast.com/t/HWLZTOJEn8T

  resultingDatatable = dataTableAllEngagementLetters.Select()
                        .Where(x => !dataTableAllInfoLists.Select(string.Format("ClientCode = '{0}' and JobCode = '{1}'", x["ClientCode"], x["JobCode"]))
                        .Any())
                        .CopyToDataTable();

最后一个告诉我源上有行!

【问题讨论】:

    标签: c# asp.net linq entity-framework datatable


    【解决方案1】:

    不是很好,但这是我想到的第一个解决方案:

    DataTable dataTable3 = dataTable2.Select().Where(x => dataTable1.Select(string.Format("clientCode = '{0}' and jobCode = '{1}'", x["clientCode"], x["jobCode"])).Count() == 0).CopyToDataTable();
    

    编辑

    Count == 0 可以改为!Any()。所以应该是这样的:

    DataTable dataTable3 = dataTable2.Select().Where(x => !dataTable1.Select(string.Format("clientCode = '{0}' and jobCode = '{1}'", x["clientCode"], x["jobCode"])).Any()).CopyToDataTable();
    

    【讨论】:

    • 如何用 .Any> 重写它?
    • 我有以下错误,T001 它实际上是一个工作代码:我在 string.format 中遗漏了什么吗? screencast.com/t/HWLZTOJEn8T
    • 我不知道你是如何命名列的,所以先检查大写字母。其次,您的 dataTableAllInfoLists 那些(clientCode 和 jobCode)列?
    • 确实是JobCode和ClientCode,驼峰式,但这并没有解决问题,我尝试在select上使用括号,但仍然是同样的问题。 screencast.com/t/UJlCXSJJ
    • 还是看不懂什么是T001?是 dataTableAllInfoLists 中的列名吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2021-11-08
    • 2018-12-08
    • 1970-01-01
    • 2011-01-05
    • 2012-04-30
    相关资源
    最近更新 更多