Solved

Linq to SQL Query with null values

Posted on 2010-11-17
1
658 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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