You're almost there.
Remove the .ToArray
call to prevent the query from being executed directly, and make your stockMaterials
variable of type IQueryable<StockMaterial>
.
+1
A:
Julien Lebosquain
2010-06-28 12:24:43
+2
A:
To get an IN query you need to reverse the sense of the contains. Also, no need to materialize the first query, IMO.
var stockMaterials = from s in stockMovementCtx.StockMaterials
where s.barcode == Barcode && s.ownership ==1
select s;
actualAmountsByLocation = (from a in stockMovementCtx.ActualAmountsByLocations
where stockMaterials.Contains( a.ItemBarcode)
select a).First();
tvanfosson
2010-06-28 12:27:27
A:
This what you're looking for?
ActualAmountsByLocation = StockMaterials.Where(s => s.barcode == Barcode && s.ownership == 1).ToArray();
Andy
2010-06-28 12:28:39
A:
var stockMaterials = (from s in stockMovementCtx.StockMaterials
where s.barcode == Barcode && s.ownership == 1
select s).ToArray();
var actualAmountsByLocation = (from a in stockMovementCtx.ActualAmountsByLocations
where stockMaterials.Contains(a.ItemBarcode)
select a).First();
Mau
2010-06-28 12:29:56
A:
Hopefully this code example below is helpful for you
// these are using linq methods
var barcodes = stockMovementCtx.StockMaterials
.Where(s => s.barcode == Barcode && s.ownership == 1)
.Select(s => s.barcode);
var amounts = stockMovementCtx.ActualAmountsByLocations
.Where(a => barcodes.Contains(a.ItemBarCode))
.FirstOrDefault();
// if you would like to use the query expressions instead, here they are
//var barcodes = from s in stockMovementCtx.StockMaterials
// where s.barcode = Barcode && s.ownership == 1
// select s.barcode;
//var amounts = (from a in stockMovementCtx.ActualAmountsByLocations
// where barcodes.Contains(a.ItemBarCode.Contains)
// select a).FirstOrDefault();
// helpful to use FirstOrDefault if you are not sure that the query will return a result
if (amounts != null) {
// change value
amounts.IsCustomerItem = 1;
// update database
stockMovementCtx.SubmitChanges();
}
Nate Pinchot
2010-06-28 12:38:17
A:
Making update on LinQ is so easy thing, check the code block below for example.
var user = (from s in dataContext.USERS
Calling Users Table, and setting where condition.
where s.UserId.Equals(id)
select s).FirstOrDefault();
I want to make change on user email.(Dont forget this: If you call with ToList(), you need to control list count to avoid exception for ex: if(user.Count > 0)..)
Updating user:
s.EmailAddress = [email protected];
dataContext.SubmitChanges();
and your data will be updated after SubmitChanges();
Serkan Hekimoglu
2010-06-29 09:04:04