How do I return NULL values after performing a LEFT JOIN in LINQ?Hidden Features of C#?Use of var keyword in C#Performance surprise with “as” and nullable typesLEFT OUTER JOIN in LINQLeft join with LINQ ErrorHow to perform Join between multiple tables in LINQ lambdaLinq to SQL Left Join, Order and Group By CountNhibernate Linq is null on left joinConverting a LINQ Inner join with Left JoinHow To Perform Left Join In Linq With Null Check?

Count rook moves 1D

What would a biological creature need in order to see the future?

Were the women of Travancore, India, taxed for covering their breasts by breast size?

First Number to Contain Each Letter

IEEE Registration Authority mac prefix

What did Boris Johnson mean when he said "extra 34 billion going into the NHS"

'This one' as a pronoun

Are treasury bonds more liquid than USD?

One hour 10 min layover in Newark; International -> Domestic connection. Enough time to clear customs?

What exactly is a softlock?

How does Harry wear the invisibility cloak?

Why would a Intel 8080 chip be destroyed if +12 V is connected before -5 V?

Global variables and information security

Which is the best password hashing algorithm in .NET Core?

Did Alan Turing's student Robin Gandy assert that Charles Babbage had no notion of a universal computing machine?

Splitting polygons at narrowest part using R?

How could it be that the capo isn't changing the pitch?

Round away from zero

How to anonymously report the Establishment Clause being broken?

Did the US Climate Reference Network Show No New Warming Since 2005 in the US?

Is it possible to observe space debris with Binoculars?

Professor refuses to write a recommendation letter

Do 643,000 Americans go bankrupt every year due to medical bills?

How did Gollum know Sauron was gathering the Haradrim to make war?



How do I return NULL values after performing a LEFT JOIN in LINQ?


Hidden Features of C#?Use of var keyword in C#Performance surprise with “as” and nullable typesLEFT OUTER JOIN in LINQLeft join with LINQ ErrorHow to perform Join between multiple tables in LINQ lambdaLinq to SQL Left Join, Order and Group By CountNhibernate Linq is null on left joinConverting a LINQ Inner join with Left JoinHow To Perform Left Join In Linq With Null Check?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?



+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+


Above is basically the SQL table returned after LEFT JOIN as expected



However using LINQ:



 var query = 
```
into temp
from t in temp.DefaultIfEmpty()
select new

```,
```,
Column3 = (int?)t.ColumnNo
;


Debugger barks at me for Column3 for:




Object reference not set to an instance of an object











share|improve this question





















  • 1





    t is null right? So you'd want to do (int?)t.?ColumnNo?

    – Brendan Green
    Mar 28 at 3:39







  • 1





    It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

    – Enigmativity
    Mar 28 at 4:14

















0















I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?



+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+


Above is basically the SQL table returned after LEFT JOIN as expected



However using LINQ:



 var query = 
```
into temp
from t in temp.DefaultIfEmpty()
select new

```,
```,
Column3 = (int?)t.ColumnNo
;


Debugger barks at me for Column3 for:




Object reference not set to an instance of an object











share|improve this question





















  • 1





    t is null right? So you'd want to do (int?)t.?ColumnNo?

    – Brendan Green
    Mar 28 at 3:39







  • 1





    It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

    – Enigmativity
    Mar 28 at 4:14













0












0








0








I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?



+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+


Above is basically the SQL table returned after LEFT JOIN as expected



However using LINQ:



 var query = 
```
into temp
from t in temp.DefaultIfEmpty()
select new

```,
```,
Column3 = (int?)t.ColumnNo
;


Debugger barks at me for Column3 for:




Object reference not set to an instance of an object











share|improve this question
















I've created a LEFT JOIN in LINQ. How do I get my query to return my nullable values?



+------+------+------+
| c1 | c2 | c3 |
|------+------+------|
| x1 | y1 | z1 |
| x2 | y2 | null |
| x3 | y3 | z1 |
+------+------+------+


Above is basically the SQL table returned after LEFT JOIN as expected



However using LINQ:



 var query = 
```
into temp
from t in temp.DefaultIfEmpty()
select new

```,
```,
Column3 = (int?)t.ColumnNo
;


Debugger barks at me for Column3 for:




Object reference not set to an instance of an object








c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 3:42









Brendan Green

9,4505 gold badges34 silver badges62 bronze badges




9,4505 gold badges34 silver badges62 bronze badges










asked Mar 28 at 3:19









QmaQma

214 bronze badges




214 bronze badges










  • 1





    t is null right? So you'd want to do (int?)t.?ColumnNo?

    – Brendan Green
    Mar 28 at 3:39







  • 1





    It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

    – Enigmativity
    Mar 28 at 4:14












  • 1





    t is null right? So you'd want to do (int?)t.?ColumnNo?

    – Brendan Green
    Mar 28 at 3:39







  • 1





    It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

    – Enigmativity
    Mar 28 at 4:14







1




1





t is null right? So you'd want to do (int?)t.?ColumnNo?

– Brendan Green
Mar 28 at 3:39






t is null right? So you'd want to do (int?)t.?ColumnNo?

– Brendan Green
Mar 28 at 3:39





1




1





It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

– Enigmativity
Mar 28 at 4:14





It seems to me that you haven't posted valid C# code. Could you give us a minimal reproducible example? That would be your source as valid C# (rather than, or in addition to, the table of data) and then you could provide a query that is valid C#.

– Enigmativity
Mar 28 at 4:14












3 Answers
3






active

oldest

votes


















0
















Because of your DefaultIfEmpty(), the resulting t can be null. And of course you can't get the ColumnNo from a null object.



The quick solution would be to use the null conditional operator::



Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?


Which has the same effect as:



Column3 = (t.ColumnNo != null) t.ColumnNo : null;


The null coalescing operator as others suggested won't work:



Column3 = (t.c3 ?? 0)


if t equals null, you can't get t.c3, and thus can't check if t.c3 equals null.






share|improve this answer

























  • Thank you, this worked for me!

    – Qma
    Mar 28 at 12:22


















0
















You can use ?? to set a default value if the column value is null:



 select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;





share|improve this answer

























  • Isn't the issue that t is null, not that c3 is null?

    – Brendan Green
    Mar 29 at 2:39


















0
















var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new

Professions = U1.ProfessionName // If null
);


You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.






share|improve this answer

























  • They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

    – Brendan Green
    Mar 29 at 2:39













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389655%2fhow-do-i-return-null-values-after-performing-a-left-join-in-linq%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0
















Because of your DefaultIfEmpty(), the resulting t can be null. And of course you can't get the ColumnNo from a null object.



The quick solution would be to use the null conditional operator::



Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?


Which has the same effect as:



Column3 = (t.ColumnNo != null) t.ColumnNo : null;


The null coalescing operator as others suggested won't work:



Column3 = (t.c3 ?? 0)


if t equals null, you can't get t.c3, and thus can't check if t.c3 equals null.






share|improve this answer

























  • Thank you, this worked for me!

    – Qma
    Mar 28 at 12:22















0
















Because of your DefaultIfEmpty(), the resulting t can be null. And of course you can't get the ColumnNo from a null object.



The quick solution would be to use the null conditional operator::



Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?


Which has the same effect as:



Column3 = (t.ColumnNo != null) t.ColumnNo : null;


The null coalescing operator as others suggested won't work:



Column3 = (t.c3 ?? 0)


if t equals null, you can't get t.c3, and thus can't check if t.c3 equals null.






share|improve this answer

























  • Thank you, this worked for me!

    – Qma
    Mar 28 at 12:22













0














0










0









Because of your DefaultIfEmpty(), the resulting t can be null. And of course you can't get the ColumnNo from a null object.



The quick solution would be to use the null conditional operator::



Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?


Which has the same effect as:



Column3 = (t.ColumnNo != null) t.ColumnNo : null;


The null coalescing operator as others suggested won't work:



Column3 = (t.c3 ?? 0)


if t equals null, you can't get t.c3, and thus can't check if t.c3 equals null.






share|improve this answer













Because of your DefaultIfEmpty(), the resulting t can be null. And of course you can't get the ColumnNo from a null object.



The quick solution would be to use the null conditional operator::



Column3 = t?.ColumnNo; // assuming ColumnNo is already an int? otherwise cast to int?


Which has the same effect as:



Column3 = (t.ColumnNo != null) t.ColumnNo : null;


The null coalescing operator as others suggested won't work:



Column3 = (t.c3 ?? 0)


if t equals null, you can't get t.c3, and thus can't check if t.c3 equals null.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 7:59









Harald CoppoolseHarald Coppoolse

14.3k3 gold badges35 silver badges69 bronze badges




14.3k3 gold badges35 silver badges69 bronze badges















  • Thank you, this worked for me!

    – Qma
    Mar 28 at 12:22

















  • Thank you, this worked for me!

    – Qma
    Mar 28 at 12:22
















Thank you, this worked for me!

– Qma
Mar 28 at 12:22





Thank you, this worked for me!

– Qma
Mar 28 at 12:22













0
















You can use ?? to set a default value if the column value is null:



 select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;





share|improve this answer

























  • Isn't the issue that t is null, not that c3 is null?

    – Brendan Green
    Mar 29 at 2:39















0
















You can use ?? to set a default value if the column value is null:



 select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;





share|improve this answer

























  • Isn't the issue that t is null, not that c3 is null?

    – Brendan Green
    Mar 29 at 2:39













0














0










0









You can use ?? to set a default value if the column value is null:



 select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;





share|improve this answer













You can use ?? to set a default value if the column value is null:



 select new
```,
```,
Column3 = (t.c3 ?? 0) // if null set its value to zero
;






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 6:16









Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani

24.1k19 gold badges72 silver badges129 bronze badges




24.1k19 gold badges72 silver badges129 bronze badges















  • Isn't the issue that t is null, not that c3 is null?

    – Brendan Green
    Mar 29 at 2:39

















  • Isn't the issue that t is null, not that c3 is null?

    – Brendan Green
    Mar 29 at 2:39
















Isn't the issue that t is null, not that c3 is null?

– Brendan Green
Mar 29 at 2:39





Isn't the issue that t is null, not that c3 is null?

– Brendan Green
Mar 29 at 2:39











0
















var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new

Professions = U1.ProfessionName // If null
);


You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.






share|improve this answer

























  • They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

    – Brendan Green
    Mar 29 at 2:39















0
















var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new

Professions = U1.ProfessionName // If null
);


You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.






share|improve this answer

























  • They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

    – Brendan Green
    Mar 29 at 2:39













0














0










0









var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new

Professions = U1.ProfessionName // If null
);


You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.






share|improve this answer













var list = (from PM in DataContext.table1
join U in DataContext.table2 on PM.Id equals U.Id into obj1
from U1 in obj1.DefaultIfEmpty()
select new

Professions = U1.ProfessionName // If null
);


You can use DefaultIfEmpty and use that object to get the column so you can get null values in column.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 8:16









Nirav VasoyaNirav Vasoya

3443 silver badges18 bronze badges




3443 silver badges18 bronze badges















  • They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

    – Brendan Green
    Mar 29 at 2:39

















  • They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

    – Brendan Green
    Mar 29 at 2:39
















They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

– Brendan Green
Mar 29 at 2:39





They are already using DefaultIfEmpty(). As with the OP, wouldn't U1 be null?

– Brendan Green
Mar 29 at 2:39

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389655%2fhow-do-i-return-null-values-after-performing-a-left-join-in-linq%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript