Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:09:23+00:00 2026-06-11T08:09:23+00:00

A simple stored procedure that I want to handle with LINQ instead: SELECT CASE

  • 0

A simple stored procedure that I want to handle with LINQ instead:

SELECT 
CASE  WHEN mg.MovieID IS NULL THEN 0 else 1 end as Selected ,
g.genreID, g.GenreName
FROM dbo.Genres g LEFT JOIN 
(
    SELECT MovieID, GenreID FROM [dbo].[MovieGenre] m 
    WHERE m.MovieID = @Movie
) MG 
ON g.[GenreID]  = mg.[GenreID]
ORDER BY g.GenreName

I think this should be simple and I think it would be a common requirement, yet I can’t figure it out nor have I found a solution via searching the web.

The app is in WPF backed by an EF model. Since EF hides the join table I need LINQ syntax that can deal with the absence of the intermediary table.

Classic many-to-many with a simple join table: table 1:Movies, table 2: Genres, Join table: MovieGenres. In the UI the user selects a specfic movie. For that movie I want to bring back ALL the genres and a bool value indicating whether the genre has been assigned to the movie. Hours of attempting this in LINQ have failed me, so the solution is currently to have the stored procedure above generate the values for me. I won’t always be abe to do this with a stored procedure and would love to see a LINQ solution.

Here’s the actual SQL table structures

CREATE TABLE [dbo].[Genres](
    [GenreID] [int] IDENTITY(1,1) NOT NULL,
    [GenreName] [nvarchar](15) NOT NULL,
 CONSTRAINT [PK_Genres] PRIMARY KEY CLUSTERED 
(
    [GenreID] ASC
)) ON [PRIMARY]

GO  

CREATE TABLE [dbo].[Movies](
    [MovieID] [int] IDENTITY(1,1) NOT NULL,
    [MovieTitle] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Movies] PRIMARY KEY CLUSTERED 
(
    [MovieID] ASC
))
ON [PRIMARY]
GO

CREATE TABLE [dbo].[MovieGenre](
    [MovieID] [int] NOT NULL,
    [GenreID] [int] NOT NULL,
 CONSTRAINT [PK_MovieGenre] PRIMARY KEY CLUSTERED 
(
    [MovieID] ASC,
    [GenreID] ASC
)) ON [PRIMARY]
GO

ALTER TABLE [dbo].[MovieGenre]  WITH CHECK ADD  CONSTRAINT [FK_Genres] FOREIGN KEY([GenreID])
REFERENCES [dbo].[Genres] ([GenreID])
GO
ALTER TABLE [dbo].[MovieGenre] CHECK CONSTRAINT [FK_Genres]
GO
ALTER TABLE [dbo].[MovieGenre]  WITH CHECK ADD  CONSTRAINT [FK_Movies] FOREIGN KEY([MovieID])
REFERENCES [dbo].[Movies] ([MovieID])
GO
ALTER TABLE [dbo].[MovieGenre] CHECK CONSTRAINT [FK_Movies]
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:09:24+00:00Added an answer on June 11, 2026 at 8:09 am

    This should do the trick.

    • use from and in with the navigation property for a join
    • DefaultIfEmpty to make it a left join
    • Using a ternary operator ? : for the case statement

    Query:

    var query = from g in context.Genres
                from m in g.Movies.Where(x => x.MovieID == movieId)
                                   .DefaultIfEmpty()
                orderby g.GenreName
                select new {
                  Selected = m == null ? 0 : 1,
                  g.genreID, 
                  g.GenreName
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I write a simple stored procedure in postgres that doesn't return a
I have a really simple stored procedure that looks like this: CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID
I have a stored procedure that is working well, it's a select query. I
I've an existing stored procedure SP1 which is simple select from a table. It's
Here's my scenario: I have a simple stored procedure that removes a specific set
I'm trying to create a simple stored procedure that stores queried result into one
I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE
I'm trying to write a fairly simple stored procedure in sql server. If the
I'm working on SQL server 2005 and I have a very simple stored procedure:
I'm looking for a way to map a simple insert stored procedure using NHibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.