Solved

Linq to SQL Query with null values

Posted on 2010-11-17
1
668 Views
Last Modified: 2013-11-11
I am trying to convert a SQL query to a Linq query, but not getting the expected results.

SQL Query
SELECT     ST.stateid, ST.statename, SM.userid
FROM         dbo.tbl_servicearea_matrix AS SM RIGHT OUTER JOIN
                      dbo.tbl_states AS ST ON SM.stateid = ST.stateid
WHERE     (SM.userid = @userid) OR
                      (SM.userid IS NULL)

Linq Query
var rsServiceArea = from ST in DB.tbl_states
                                join SM in DB.ServiceAreas on ST.stateid equals SM.stateid                                
                                where SM.userid == IMOSAPI.GetActiveUserID || SM.userid == null
                                select new {ST.stateid,ST.statename,SM.userid};
        }

My SQL query returns all the states and the Linq only returns the states with userid.
0
Comment
Question by:CodeMarx
1 Comment
 

Accepted Solution

by:
CodeMarx earned 0 total points
ID: 34158740
Got it by doing this...

var rsServiceArea = from ST in DB.tbl_states
                                join SM in DB.ServiceAreas on ST.stateid equals SM.stateid into SAM
                                from SM in SAM.DefaultIfEmpty()
                                where SM.userid == IMOSAPI.SetActiveUserID || SM.userid == null
                                select new {
                                    StateID = ST.stateid,
                                    StateName = ST.statename,
                                    Selected = SM == null ? false : true};
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question