CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2013
    Posts
    19

    Unhappy How to query common values in Linq to SQL?

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

    Name:  tables.jpg
Views: 772
Size:  36.2 KB

    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:

    Code:
    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 !!!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to query common values in Linq to SQL?

    Separate it into 2 different queries. Once the first one is done, use it against the second query. Look for 101 LINQ Samples on the Web


    Or Select.Unique? Might exist
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2013
    Posts
    19

    Re: How to query common values in Linq to SQL?

    Thanks dglienna !!!

    But it is hard to believe to me that this post has been viewed for about 92 persons and no body knows how to do this 'simple' SQL query or not want to.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured