Custom DataSet.Table.FindBy method
I have a strongly-typed DataSet that was created using visual studio 2010's Configuration Wizard. I can find a DataRow easily as long as I know the primary key (see How to: Edit Rows in a DataTable).
The problem occurs when I don't know the PK. Is there a way to create a custom method that returns a DataRow if you have a combination of columns that could also be a composite primary key (unique constraint). Using the example from the link, I would like to do something like this:
NorthwindDataSet.CustomersRow customersRow = northwindDataSet1.Customers.FindByCustomerNameAndType("TestCustomerName", "TestCustomerType");
This assumes开发者_运维知识库 their Northwind DB Customers Table has two columns (name and type) that could also be a composite key. And the the FindBYCustomerNameAndType method would map to
SELECT *
FROM Customers
WHERE name = "TestCustomerName" AND type = "TestCustomerType"
string whereClause = "name = 'TestCustomerName' and type = 'TestCustomerType'";
DataRow[] x = northwindDataSet1.Customers.Select(whereClause);
if (x.Length > 0){
CustomersRow customersRow = x[0] as CustomersRow;
//other code here
}
精彩评论