views:

90

answers:

2

Using Oracle® Data Provider for .NET to generate strongly typed datasets.

I could of course fill the entire table, but I would like to learn how to use the Where() extension with a delegate function that should limit number of collected rows based on certain table values.

Parameters for Where() extension:

(this IEnumerable<CUSTOMERRow> source, Func<CUSTOMERRow, bool> predicate)

The codesnippet where the delegate should be used:

StronglyTypedDataSet myDataSet = new StronglyTypedDataSet();

CUSTOMERTableAdapter tableAdapter = new CUSTOMERTableAdapter();
tableAdapter.Fill(myDataSet.CUSTOMER.Where(newfunctionhere));
+1  A: 

In C# a lambda expression is typically used as the function. I.E.:

tableAdapter.Fill(myDataSet.CUSTOMER.Where(c => c.LastName == "Smith"));
Matthew Sposato
Won't work... A similar answer was already posted and deleted
Thomas Levesque
@Matthew Sposato: I get the error message: "Argument type System.Data.EnumerableRowCollection<CUSTOMERRow> is not assignable to parameter type CUSTOMERDataTable". Any ideas?
Kb
Fill takes a DataTable as a parameter. Where returns an EnumerableRowCollection<TRow>, not a DataTable. Anyway, what you're trying to do makes no sense at all. You're trying to filter a DataTable that's empty anyway... The Where method filters the DataTable, NOT the query made by the table adapter !
Thomas Levesque
+1  A: 

The Where method can be used to filter the contents of a collection. But in your case you're using a table adapter to fill an empty DataTable, and calling Where on an empty collection just yields an empty sequence... And the table adapter doesn't know how to interpret the Where call, it just uses its SelectCommand to fill the table. So you can't use Linq to define which data you want to load in the table. But once your table is filled, then you can use Where to filter the results, like that :

var rows = myDataSet.CUSTOMER.AsEnumerable().Where(cr => cr.SomeProperty == someValue);
Thomas Levesque
@Thomas Levesque: I get the error message: "Argument type System.Data.EnumerableRowCollection<CUSTOMERRow> is not assignable to parameter type CUSTOMERDataTable". Any ideas?
Kb
@Kb: you cannot assign the result of the `Where()` query on a rows of the table back to your `DataTable` variable. The result of `Where` is `IEnumerable` of rows, not a table itself.
Pavel Minaev
@Pavel Minaev: So I cannot use the Where() exstension inside tableadapter.fill()?
Kb
No you can't. That's what I tried to explain, but I seems I failed miserably ;)
Thomas Levesque
@Thomas Levesque: Thanks. Never give up. Green marked! ;))
Kb