Solved

Linq to SQL Query with null values

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now