将IQueryable转换为DataRowView

本文关键字:DataRowView 转换 IQueryable | 更新日期: 2023-09-27 18:05:09

我有一个绑定源,它是由一个可查询的LINQ到SQL的结果。

private void loadProducts(short _engineID)
{    
     dbLinqDataContext db = new dbLinqDataContext();                   
     var  productQuery = from pe in db.productEngines
                         where pe.EngineID == _engineID
                         select new
                         {
                           ProductID = pe.ProductID,
                           ProductNumber = pe.product.ProductNumber,
                           Description = pe.product.ProductName,
                           Cost = String.Format("{0:C}", pe.product.StandardCost),
                           Price = String.Format("{0:C}", pe.product.ListPrice),
                           QOH = String.Format("{0:0.00}",pe.product.QOH),
                           Picture = pe.product.Picture,
                           Category =pe.product.category.Category1,   
                           Engine=pe.engine.ModelSeriesYear,
                           Manufacturer=pe.engine.manufacturer.Manufacturer
                        };

productQueryBindingSource。数据源= productQuery;}

然后我无法将我的bindingsource当前项目转换为DataViewRow。

DataRow currentRow = ((DataRowView)productQueryBindingSource.Current).Row;

我如何将我的匿名类型转换为DataViewRow类型?

将IQueryable转换为DataRowView

要将Anonymous转换为DataRow,应该使用反射。更多信息请查看下面的链接

将自定义集合转换为数据表