By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

How to query common values in Linq to SQL?

P: 29
We have two tables PRODUCTS and CATEGORIES, both of them have a many-to-many relationship through a third table --> CATE_PRODUCTS

My question: given 1 or more ID_PRODUCT I need to retrieve all the COMMON Categories that belongs to those Products.

Example: Given 3 Products IDs:

PRODUCTS ---------- CATEGORIES of each one
COKE ------------> SelfService - Sale - **Beverage**
ORANGE JUICE ----> **Beverage**
VINE ------------> Licour - **Beverage**

In this case 'Beverage' is the only one common category value for those 3 given Products

I'm trying to do it through Linq To SQL in VB.NET, but not successful

With the code below I retrieve all Categories from all given Products:

Expand|Select|Wrap|Line Numbers
  1. Dim Query = (From Cate In db.CATEGORIAS Join CatePro In db.CATE_PRODUCTOS On Cate.ID_CATEGORIA Equals CatePro.ID_CATEGORIA Where MiList.Contains(CatePro.ID_PRODUCTO) Select Cate.CAT_NOMBRE)
But I need to retrieve ONLY the common Categories, not all Categories.

żAny ideas?

Thanks a lot !!!
Attached Images
File Type: jpg tables.jpg (25.3 KB, 60 views)
Feb 2 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.