+1  A: 

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>.

Julien Lebosquain
+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
A: 

This what you're looking for?

ActualAmountsByLocation = StockMaterials.Where(s => s.barcode == Barcode && s.ownership == 1).ToArray();
Andy
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
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
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