LINQ to SQL query question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nightcrawler

    LINQ to SQL query question

    I have the following tables that I have dragged into a .dbml file
    (have only included the keys for simplicity).

    CREATE TABLE [dbo].[Catalog](
    [CatalogId] [int] NOT NULL)


    CREATE TABLE [dbo].[CatalogFavorite](
    [UserId] [uniqueidentifie r] NOT NULL,
    [CatalogId] [int] NOT NULL)

    A user can add catalog items to their favorites. A user can also
    browse other users favorites.

    I am trying to write a LINQ to SQL query where I am seeing another
    users favorites but I would also like to see if I have any of those
    favorites in my own favorites.

    The following query would return all favorites for a particular user.

    SELECT *
    FROM CatalogFavorite
    WHERE UserId = uniqueidentifie r of the user I am viewing'

    How can I rewrite the query to see if any of that users favorites are
    in my favorites (I have my uniqueidentifer available for this query).
    I assume I need another column (maybe of type bit) where the query
    would return a true if I have the item in my favorites as well or a
    false if I don't.

    Any help would be highly appreciated.

    Thanks
  • Nightcrawler

    #2
    Re: LINQ to SQL query question

    I might want to add that in SQL I would do something like:

    SELECT Catalog.Catalog Id, SUM(CASE WHEN CatalogFavorite _1.UserId =
    'my uniqueidentifie r' THEN 1 ELSE 0 END) AS HasFavorite
    FROM CatalogFavorite INNER JOIN
    Catalog ON CatalogFavorite .CatalogId = Catalog.Catalog Id INNER JOIN
    CatalogFavorite AS CatalogFavorite _1 ON Catalog.Catalog Id =
    CatalogFavorite _1.CatalogId
    WHERE (CatalogFavorit e.UserId = 'uniqueidentifi er of the user I am
    viewing')
    GROUP BY Catalog.Catalog Id

    Not sure how this would be done in LINQ.

    Thanks

    Comment

    Working...