[英]How to do self-referential select on hierarchical data in DataTable?
我有一個DataTable,假設有兩列:ReportId和ParentReportId。 我想選擇所有父報表尚未在數據表中列出的報表。 我想做這樣的事情(取自SQL語法),但是我知道這行不通:
DataRow[] foo = drParentRow = dt.Select("ParentReportId NOT IN(ReportId)");
在這種情況下,如何從該數據集中獲取這些記錄?
var noParents = dt.AsEnumerable().Where(x => x["ReportParentID"] != DBNull.Value &&
!dt.rows.Any(y => y["ReportID"] == x["ReportParentID"]);
我認為您將為此使用一些LINQ:
var reportIds = dt.AsEnumerable().Select(row => row.Field<int>("ReportId"));
var foo = from row in dt.AsEnumerable()
let parentReportId = row.Field<int>("ParentReportId")
where !reportIds.Contains(parentReportId)
select row;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.