Determine if foreign key table has any links to primary key table with LINQ & DataTable
I have 2 datatables.
One is the master the other is the detail
When someone goes to del开发者_JAVA百科ete a master record how can I check that there are no rows in my detail datatable that relate to the master table's ID.
UPDATE: The user has the ability to select more than one master record at a time
I want to use LINQ if possible.
I started looking at DataTable.Rows.Cast()......
You don't need LINQ for this.
Instead, you can check masterRow.GetChildRows("RelationName").Length
.
EDIT: You should use a DataRelation.
If you really don't want to, you can check
childTable.AsEnumerable().Any(dr => dr["ParentIdColumn"] == someValue)
EDIT: To check for multiple parents:
var parentKeys = parentRows.Select(dr => dr["id"]).ToList();
if (childTable.AsEnumerable().Any(dr => parentKeys.Contains(["ParentIdColumn"])))
精彩评论