How can I search rows in a datatable for a row with Col1=”MyValue”
I’m thinking something like
Assert.IsTrue(dataSet.Tables[0].Rows.
FindAll(x => x.Col1 == "MyValue" ).Count == 1);
But of course that doesn’t work!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use LINQ to DataSets to do this:
Note, you can also do this without the call to Assert:
If the number of rows does not equal one (hence, the call to
Single), then an exception will be thrown, and that unhandled exception should fail your test case. Personally, I like the latter, as it has a clearer semantic meaning.The above can be further whittled down to:
Additionally, you can take advantage of the
Fieldmethod on theDataRowExtensionsclass to simplify type-safe access to the field (as well as providing the extra benefit of convertingDBNullto null counterparts in .NET):