My table has tow columns id, parentid and I want to filter data to select rows which have parentid ( as there can be rows without parentid row).
id parentid 1 null 2 1 3 1 4 2 5 10
I want all the rows except 5, 10 as 10 is not there as a parent (no children unless parent is there).
I want rows with parentid as I am using the data for treeview. I have tried couple of ways but not getting what I want.
var mycol = from t in ds.Tables[0].AsEnumerable()
where t.Field<int>("Id") == t.Fields<int>("ParentId");
select t;
or
DataRow[] drs = ds.Tables[0].Select("ParentId in Id");
To get rows with ParentId
not null, use
var rows = from r in ds.Tables[0].Rows.AsEnumerable()
where !r.IsNull("ParentId")
select r;
or
var rows = ds.Tables[0].Rows.AsEnumerable().Where(r => !r.IsNull("ParentId"));
It's unclear what you want.
If you want rows where ParentId is not null, use one of the other answers.
If you want rows where Id is used as a ParentId somewhere in the dataset, use something like this:
var table = ds.Tables[0];
var parentIds = new HashSet<int>(
from row in table.AsEnumerable()
let parentId = row.Field<int?>("ParentId")
where parentId != null
select parentId.Value);
var rowsWhereIdIsAParentId =
from row in table.AsEnumerable()
let id = row.Field<int>("Id")
where parentIds.Contains(id)
select row;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.