none
Join With Multiple tables RRS feed

  • Question

  • Hello ,

      I write the following query  :

      var query = from up in dc.User_Products
                         join user in dc.Users on up.UserId equals user.UserId
                         join product in dc.Products on up.Product equals product.ProductId
                         select new
                            {
                                UserName = user.UserName
                            };

    it gives me build error "The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'."

     

    I need to write query to get the following statements:

     "select Users.UserName , User_Products.Status  from  User_Products

       inner join Users on Users.UserId=User_Products.UserId

       inner join Products on Products.ProductId = User_Products.ProductId"

      Thanks ,


    ASk
    Monday, September 26, 2011 9:39 AM

Answers

  • Hi Himo,

    I have made many linq joins like yours before and it always worked.

    One thing that I have noticed, in your second join you are writing:

     up.Product equals product.ProductId
    

    should not the first be ProductId?

     up.ProductId equals product.ProductId
    

    So the query looks like:

     

      var query = from up in dc.User_Products
                         join user in dc.Users on up.UserId equals user.UserId
                         join product in dc.Products on up.ProductId equals product.ProductId
                         select new
                            {
                                UserName = user.UserName
                            };
    


     


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    • Marked as answer by himo Monday, September 26, 2011 5:26 PM
    Monday, September 26, 2011 12:13 PM

All replies

  • Hi,

    Try with something like this:

    var query = from user in dc.Users
                from up in dc.User_Products
                select new 
                { 
                    user.UserName,  
                    up.Status 
                } 
    
    

    For more details, take a look at this link:

    http://blogs.teamb.com/craigstuntz/2010/01/13/38525/

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Proposed as answer by BoovendanM Monday, September 26, 2011 12:11 PM
    Monday, September 26, 2011 11:26 AM
  • Hi Himo,

    I have made many linq joins like yours before and it always worked.

    One thing that I have noticed, in your second join you are writing:

     up.Product equals product.ProductId
    

    should not the first be ProductId?

     up.ProductId equals product.ProductId
    

    So the query looks like:

     

      var query = from up in dc.User_Products
                         join user in dc.Users on up.UserId equals user.UserId
                         join product in dc.Products on up.ProductId equals product.ProductId
                         select new
                            {
                                UserName = user.UserName
                            };
    


     


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    • Marked as answer by himo Monday, September 26, 2011 5:26 PM
    Monday, September 26, 2011 12:13 PM
  • Consider the code below..

     var query = from up in dc.User_Products
                         join user in dc.Users on user.UserId equals up.UserId 
                         join product in dc.Products on product.ProductId equals up.Product 
                         select new
                            {
                                UserName = user.UserName
                            };


    With Best Regards, Boovendan M
    Monday, September 26, 2011 12:20 PM