LINQ to SQL query question

N

Nightcrawler

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] [uniqueidentifier] 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 = uniqueidentifier 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
 
N

Nightcrawler

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

SELECT Catalog.CatalogId, SUM(CASE WHEN CatalogFavorite_1.UserId =
'my uniqueidentifier' THEN 1 ELSE 0 END) AS HasFavorite
FROM CatalogFavorite INNER JOIN
Catalog ON CatalogFavorite.CatalogId = Catalog.CatalogId INNER JOIN
CatalogFavorite AS CatalogFavorite_1 ON Catalog.CatalogId =
CatalogFavorite_1.CatalogId
WHERE (CatalogFavorite.UserId = 'uniqueidentifier of the user I am
viewing')
GROUP BY Catalog.CatalogId

Not sure how this would be done in LINQ.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top