LINQ を使用して 2 つの DataTable に対して一致しないクエリを実行するメソッドがあります。オンラインで見回してエラーが発生していると思われる場所を特定したというエラーが生成されていますが、それを修正する方法がわかりません。
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;
}