Taking advantage of Linq trying to make a neater update code on the provided DataSet-Based approach :
The Code I'm trying is :
ListProducts.ForEach( product =>
{
DsProducts.TblProductsRow row = Ds1Products.TblProducts.First( p => p.Name == product.Name);
row.Price = Product.Price ;
});
Adapter.Update(Ds1Products, "TblProducts");
Not sure weather I needed parameters here or not, I haven't defined any yet. Cause wanted to use the "Typed" facilities of it. The Data Adapter is initialized and filled.
Notes :
It does not work right now , Gotten Error here : "Sequence contains no matching element"
Wanted to Replace the foreach with Linq's Select
if possible
( Try it to be neat and Linqy as possible )
its in SQL CE (Shouldn't be a problem here on most cases)
This is hopefully what you need:
var productRows = from p in ListProducts
join row in Ds1Products.Tables["TblProducts"].AsEnumerable()
on p.Name equals row.Field<String>("Name")
select new { NewPrice = p.Price, Row = row };
foreach(var productInfo in productRows)
{
productInfo.Row.SetField<Double>("Price", productInfo.NewPrice);
}
Edit: Here's the strong typed DataSet way(almost identical):
var productRows = from p in ListProducts
join row in Ds1Products.TblProduct
on p.Name equals row.Name
select new { NewPrice = p.Price, Row = row };
foreach (var productInfo in productRows)
{
productInfo.Row.Price = productInfo.NewPrice;
}
Adapter.Update(Ds1Products, "TblProducts");
By the way, List.ForEach
is not Linq, it existed already in .NET 2.0 hence prior to Linq.