Laden...

constraint.Columns.Contains ohne Linq

Erstellt von api vor 8 Jahren Letzter Beitrag vor 8 Jahren 736 Views
A
api Themenstarter:in
10 Beiträge seit 2011
vor 8 Jahren
constraint.Columns.Contains ohne Linq

Hallo,

mit folgender Funktion überprüfe ich, ob eine DataColumn einen "ForeignKeyConstraint" hat.
Nun müsste ich diesen Code

constraint.Columns.Contains(column)

on Linq schreiben, da dieser Namespace in einer Scriptengine nicht zur Verfügung steht.
Wie schreibe ich das am einfachsten um?

private static bool ColumnIsForeignKey(DataColumn column)
{
  // Ensure a valid column was received that actually belongs to a table
  if (column == null)
    throw new ArgumentNullException("column");
  if (column.Table == null)
    throw new ArgumentException("Column provided must belong to a table", "column");

  bool hasForeignKey = false;

  // Loop through ALL constraints
  int counter = 0;
  while ((!hasForeignKey) && (counter < column.Table.Constraints.Count))
  {
    // Filter to only ForeignKeyConstraints that include the column we were given
    ForeignKeyConstraint constraint = column.Table.Constraints[counter] as ForeignKeyConstraint;
    if ((constraint != null) && (constraint.Columns.Contains(column)))
    {
      hasForeignKey = true;
    }
    counter++;
  }

  return hasForeignKey;
}
3.170 Beiträge seit 2006
vor 8 Jahren

Hallo,

da es sich bei den Columns um ein Array handelt, könntest Du einfach IndexOf verwenden, etwa so (nicht getestet):

    if ((constraint != null) && (Array.IndexOf(constraint.Columns, column) >= 0))
    {
      hasForeignKey = true;
    }

Gruß, MarsStein

Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca