LINQ to SQL - Help needed in writing a query

Last post 06-11-2008 8:29 AM by anzer. 1 replies.

Sort Posts:

  • LINQ to SQL - Help needed in writing a query

    06-10-2008, 2:34 PM
    • Loading...
    • anzer
    • Joined on 10-19-2004, 12:00 AM
    • UAE
    • Posts 855

    I am new to LINQ and I have managed to write some basic queries. But now I have somewhat complex requirement and I spend lot of time on it but couldnt solve myself.. Please help

    I have 5 tables - The tablke names and fields are given below

    1. Category [CID(pk), Name]
    2. QuestionCategory [CID(fk), QID(fk)]
    3. Questions [QID(pk), Question, UID(fk)]
    4. Choices [ChID(pk), QID(fk), Choice]

    5. Users [UID(pk), Name]
    6. UserFavourites [UID(fk), QID(fk)]

    I have 6 tables in my QuestionBank DB. Now I want to query the Questions and its details according to a CategoryID


    The result should contain following
    1. All the details about the question
    2. All the choices under the question (using the UID fk in Choices table)
    3. The details of the user who added the question (using the UID fk in Question table)
    4. Whether the current logged in user added this Question to his favourite list. (using the UID fk and QID fk of UserFavourites table). The logged in users ID will be available in a variable.

    If this post was useful to you, please mark it as answer.

    My Blog | Free books Microsoft
    Filed under: ,
  • Re: LINQ to SQL - Help needed in writing a query

    06-11-2008, 8:29 AM
    • Loading...
    • anzer
    • Joined on 10-19-2004, 12:00 AM
    • UAE
    • Posts 855

    Somebody please help me.. I can't figure out a LINQ solution yet..

    This is the SQL I want to implement.. Please help

    SELECT *
    FROM Questions q
    JOIN QuestionCategory qc ON q.QID = qc.QID AND qc.CID = @CategoryID /*fetch all questions under the selected category*/
    JOIN Choices ch ON ch.QID = q.QID    /*fetch all choices for each question*/
    JOIN Users u ON u.UID = Q.UID        /*fetch the User who added each question*/
    LEFT JOIN UserFavourites uf ON uf.QID = q.QID AND uf.UID = @LoggedinUserID /*get the favourites of currently logged in user*/
     

     

    If this post was useful to you, please mark it as answer.

    My Blog | Free books Microsoft
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter