1

The query:-

    select * from Product.Barcodes where Barcode='00A000000O' and  
 Barcode not in(select Barcode from Sales.SalePointItem)

I have tried the following, but these does not work:-

db.Barcodes.SingleOrDefault(b => b.Barcode.Equals(ItemBarcode) &&  
  !db.SalePointItems.Select(m=>m.Barcode).Contains(b.Barcode));

db.Barcodes.SingleOrDefault(b => b.Barcode.Equals(ItemBarcode) &&  
  !db.SalePointItems.Any(m=>m.Barcode.Equals(b.Barcode)));

I get the following exception on running both of above:-

Unable to create a constant value of type 'SalePointItemModel'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

1
  • What do you mean does not work? Are you getting the wrong result? Or is the generated SQL not what you expect? What is the generated SQL? Commented May 12, 2012 at 15:48

1 Answer 1

1

Try this one:

var item = (from b in db.Barcodes
            where b.Barcode == ItemBarcode
            where !db.SalePointItems.Any(m => m.Barcode == b.Barcode)
            select b).SingleOrDefault();

Method Syntax -

var item = db.Barcodes.Where(b => b.Barcode == ItemBarcode)
                      .Where(b => !db.SalePointItems.Any(m => m.Barcode == b.Barcode))
                      .SingleOrDefault();
Sign up to request clarification or add additional context in comments.

4 Comments

I know this method, but I don't want a separate query, I wanted to run it as a subquery. See the title of the question as I have updated it.
The intent is to avoid fetching the list of barcodes into memory which the origninal t-sql does.
Actually, this works, but I was looking for extension method chains, because I usually write in that form, all my queries.
@puretechy - I added extension method way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.