Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linq to SQL Query with null values

Posted on 2010-11-17
1
Medium Priority
?
688 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

886 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