DataTable.Select with custom class comparision

  • Thread starter Thread starter Nuno Magalhaes
  • Start date Start date
N

Nuno Magalhaes

Hello,

I have a DataTable in which the items are of type MyClass.
How can I use Select to compare a MyClass instance with the DataTable?
Is there any overriden method (like the ToString()) to use it on
Select?

Thank you,
Nuno Magalhães.
 
Nuno said:
I have a DataTable in which the items are of type MyClass.
How can I use Select to compare a MyClass instance with the DataTable?
Is there any overriden method (like the ToString()) to use it on
Select?
The DataTable .Select() can only be used to select items based on column
values. You can use the "Convert" operator to cast columns, but you cannot
operate on entire rows. You'll have to do it manually by enumerating the rows.

If you have .NET 3.5 you can just .Cast() the elements and use the LINQ
..Select() rather than the DataTable .Select(), which is more powerful and
gives you compile-time checking.
 
Thanks for the reply.
So the unique method (using framework 1.1 and datatable) is to do
enumerate the rows and compare manually?
Suppose I have a class MyClass (and just want to compare one attribute
of that class) how can I use the Convert. Can you show me an example?

Thanks,
Nuno Magalhães.
 
Nuno said:
So the unique method (using framework 1.1 and datatable) is to do
enumerate the rows and compare manually?
Suppose I have a class MyClass (and just want to compare one attribute
of that class) how can I use the Convert. Can you show me an example?
Convert operates on columns. If the comparison you want to make is on a
column value, you can use .Select(). If it's on a custom property of MyClass
(not a column) you can't use .Select().
 
Back
Top