[Solved] How to use WHERE in LINQ? [closed]


I think you should try using CopyToDataTable

ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>("productID")==23).CopyToDataTable();

When you convert it to an Enumerable the filtered list has no identifiers for say ProductID hence the grid would fail to map the column and fail.

Alternatively you may also choose to use AsDataView() instead of CopyToDataTable().

A more detailed explanation can found Binding DataRows

Databinding is controlled by the ListBindingHelper and TypeDescriptor
classes. When you bind to a list, the
ListBindingHelper.GetListItemProperties method is called to get the
columns in the list. If the list implements the ITypedList interface,
its GetItemProperties method is called. Otherwise, it will use
TypeDescriptor to get the properties of the first item in the list.
(this uses reflection)

The DataView class (which DataTable also binds through, using
IListSource) implements ITypedList and returns
DataColumnPropertyDescriptors that expose the columns in the table.
This is why you can bind to a DataView or DataTable and see columns.
However, when you bind to a List, there is no ITypedList that
can return the columns as properties. It therefore falls back on
reflection and shows the physical properties of the DataRow class.

2

solved How to use WHERE in LINQ? [closed]