Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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# IEnumerable的显式转换_C#_Linq_Join - Fatal编程技术网

C# IEnumerable的显式转换

C# IEnumerable的显式转换,c#,linq,join,C#,Linq,Join,我有一个使用LINQ对两个数据表执行不匹配查询的方法。它产生了一个错误,通过在线查看,我已经确定了错误发生的位置,但我不知道如何修复它 public IEnumerable<int> UnbilledAuditKeys(DataTable audits, string keyFieldName) { var billedAudits = from x in this.GetBilledAudits().AsEnumerable() select

我有一个使用LINQ对两个数据表执行不匹配查询的方法。它产生了一个错误,通过在线查看,我已经确定了错误发生的位置,但我不知道如何修复它

public IEnumerable<int> UnbilledAuditKeys(DataTable audits, string keyFieldName) {
    var billedAudits =
        from x in this.GetBilledAudits().AsEnumerable()
        select new {
            k = x.Field<int>(keyFieldName)
        };

    var allAudits =
        from x in audits.AsEnumerable()
        select new {
            k = x.Field<int>(keyFieldName)
        };

    var unbilled =
        from a in allAudits
        join b in billedAudits on a.k equals b.k
            into combined
        from c in combined.DefaultIfEmpty()
        where c == null
        select new { // This is what's causing the error (I think)
            k = a.k
        };

    return unbilled; // This line the compiler is rejecting
}
返回的错误是

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)
我不知道如何修理它。我尝试过将整个LINQ表达式强制转换为IEnumerable这样明显的方法,但这会生成一个运行时异常

任何想法都将不胜感激

编辑:

最后一种方法是:

public IEnumerable<int> UnbilledAuditKeys(DataTable rosliAudits, string keyFieldName) {
    var billed = this.GetBilledAudits().AsEnumerable().Select(x => x.Field<int>(keyFieldName));
    var allaudits = rosliAudits.AsEnumerable().Select(x => x.Field<int>(keyFieldName));
    var unbilled = allaudits.Except(billed);
    return unbilled;
}

直接选择字段,而不是创建新的匿名类型:

var unbilled =
    from a in allAudits
    join b in billedAudits on a.k equals b.k
        into combined
    from c in combined.DefaultIfEmpty()
    where c == null
    select a.k;
简单修复:

var unbilled =
    from a in allAudits
    join b in billedAudits on a.k equals b.k
        into combined
    from c in combined.DefaultIfEmpty()
    where c == null
    select a.k;
另外,另外两个查询似乎不需要匿名结构,最后一个查询可以大大简化:

public IEnumerable<int> UnbilledAuditKeys(DataTable audits, string keyFieldName) {
    var billedAudits =
        from x in this.GetBilledAudits().AsEnumerable()
        select x.Field<int>(keyFieldName);

    var allAudits =
        from x in audits.AsEnumerable()
        select x.Field<int>(keyFieldName);

    var unbilled = allAudits.Except(billedAudits); // LINQ has many useful methods like this

    return unbilled;
}

我爱林克!!!但我对它是如此陌生,以至于我仍然像对待SQL一样对待它。非常感谢你!这样做非常有效: