It's not clear to me from your example whether you want to return true
if any of the inputs are found in any row, or if they all have to be found in some row.
The former is simpler, so here's an example of that:
static bool Conflicting(string table, params Tuple<string, string>[] columnValuePairs)
{
DataTable dt = state.Tables[table];
return dt.AsEnumerable().Any(
row => columnValuePairs.Any(p => row.Field<string>(p.Item1) == p.Item2));
}
You can call the above like this:
Conflicting("MyTable",
Tuple.Create("Col1", "A1"),
Tuple.Create("Col2", "B1"),
Tuple.Create("Col3", "C1"),
Tuple.Create("Col4", "D1"));
If the above isn't the criteria you're actually looking for, please edit your question so that it is clear what criteria you do want. Be very specific and remember that a set of examples does not uniquely define some criteria. If you're unsure what I mean, please read https://stackoverflow.com/help/how-to-ask for hints on being more specific.
EDIT:
Based on your updated question, in which you indicate you want the latter behavior, i.e. that each of the given column/value pairs must be found in some row, the following should work instead:
static bool Conflicting(string table, params Tuple<string, string>[] columnValuePairs)
{
DataTable dt = state.Tables[table];
var remainingPairs = new List<Tuple<string, string>>(columnValuePairs);
foreach (var row in dt.AsEnumerable())
{
int i = 0;
while (i < remainingPairs.Count)
{
Tuple<string, string> columnValuePair = remainingPairs[i];
if (row.Field<string>(columnValuePair.Item1) == columnValuePair.Item2)
{
remainingPairs.RemoveAt(i);
continue;
}
i++:
}
if (remainingPairs.Count == 0)
{
return true;
}
}
return false;
}
The above inspects each row to find any match column/value pairs for that row, removing that pair from the list of pairs to find if it does match. If and when the list of column/value pairs to look for becomes empty, the method returns true
, having found all of the requested pairs. If all of the rows in the table are enumerated without emptying the list, then some pair did not match any of the rows, and the method returns false
.
Note that the above is hardly the only way to implement this. Indeed, if you expected to have a very large number of column/value pairs, for performance you might want something different, such as to use a hash set or dictionary (it is possible to use either, but they each have their specific pros and cons). But for a relatively small number (say dozens at the most) of column/value pairs, a list should suffice and it does keep the implementation simple.