LINQ查询数据表以检查记录是否存在

问题描述:

我想对称为Records的数据表执行LINQ查询,并检查是否存在记录.如果存在,我想找出它所在的行.我该怎么做?

I want to perform a LINQ query on a datatable called Records and check if a record exists. If it exists, I want to find out the row which it is in. How might I go about doing this?

我想在添加system.linq命名空间后在数据表上执行一个.where,但是该方法似乎不存在.请指教

I wanted to do a .where on my datatable after adding the system.linq namespace but the method didnt seem to exist. Please advise

P.S:在2010年版中使用c#

P.S : Am using c# in vs 2010

您不能使用该方法,因为DataRowCollection没有实现IEnumerable<T>.您需要使用AsEnumerable()扩展名:

You cannot use the method because DataRowCollection doesn't implement IEnumerable<T>. You need to use the AsEnumerable() extension:

var dataRowQuery= myDataTable.AsEnumerable().Where(row => ...

您可能还需要一个对System.Data.DataSetExtensions的项目引用,才能正常工作.

You might also need a project reference to System.Data.DataSetExtensions for this to work.

祝你好运!