IsNull in LINQ Sum()
I have problem with IsNull in LINQ :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
p.LoadingsDetails.Sum(n => n.Quantity) possibility NULL. I want to have result like this :
select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails
I have tried something like :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
It return errors :
System.NotSupportedException: The method 'First' can only be used as a
final query operation. Consider using the method 'FirstOrDefault' in
this instance instead.
I have tried something like this :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
OR
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
Return Errors :
Operator '==' cannot be applied to operands of type 'decimal' and 'DBNull'

c#
add a comment |
I have problem with IsNull in LINQ :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
p.LoadingsDetails.Sum(n => n.Quantity) possibility NULL. I want to have result like this :
select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails
I have tried something like :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
It return errors :
System.NotSupportedException: The method 'First' can only be used as a
final query operation. Consider using the method 'FirstOrDefault' in
this instance instead.
I have tried something like this :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
OR
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
Return Errors :
Operator '==' cannot be applied to operands of type 'decimal' and 'DBNull'

c#
why don't you checkp.LoadingDetails.First() != nullinsideWhere()?
– Mohamad Armoon
Dec 30 '18 at 4:37
@MohamadArmoon because it isLEFT JOIN, I want to havep.Quantityif theRIGHTtableIS NULL
– Jun Rikson
Dec 30 '18 at 4:40
What is null ?p.LoadingsDetailsorn.Quantityin your LINQ expression ? Share the relevant parts of your entity class definition
– Shyju
Dec 30 '18 at 5:00
@Shyju :p.LoadingsDetails
– Jun Rikson
Dec 30 '18 at 5:01
add a comment |
I have problem with IsNull in LINQ :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
p.LoadingsDetails.Sum(n => n.Quantity) possibility NULL. I want to have result like this :
select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails
I have tried something like :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
It return errors :
System.NotSupportedException: The method 'First' can only be used as a
final query operation. Consider using the method 'FirstOrDefault' in
this instance instead.
I have tried something like this :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
OR
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
Return Errors :
Operator '==' cannot be applied to operands of type 'decimal' and 'DBNull'

c#
I have problem with IsNull in LINQ :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
p.LoadingsDetails.Sum(n => n.Quantity) possibility NULL. I want to have result like this :
select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails
I have tried something like :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
It return errors :
System.NotSupportedException: The method 'First' can only be used as a
final query operation. Consider using the method 'FirstOrDefault' in
this instance instead.
I have tried something like this :
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
OR
db.WarehouseInputsDetails
.Select(p => new WarehouseInputDetailsViewModel
{
Id = p.Id
RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
}).AsQueryable();
Return Errors :
Operator '==' cannot be applied to operands of type 'decimal' and 'DBNull'

c#
c#
edited Jan 2 at 2:15
Tetsuya Yamamoto
15.3k42140
15.3k42140
asked Dec 30 '18 at 4:29
Jun RiksonJun Rikson
1,55311330
1,55311330
why don't you checkp.LoadingDetails.First() != nullinsideWhere()?
– Mohamad Armoon
Dec 30 '18 at 4:37
@MohamadArmoon because it isLEFT JOIN, I want to havep.Quantityif theRIGHTtableIS NULL
– Jun Rikson
Dec 30 '18 at 4:40
What is null ?p.LoadingsDetailsorn.Quantityin your LINQ expression ? Share the relevant parts of your entity class definition
– Shyju
Dec 30 '18 at 5:00
@Shyju :p.LoadingsDetails
– Jun Rikson
Dec 30 '18 at 5:01
add a comment |
why don't you checkp.LoadingDetails.First() != nullinsideWhere()?
– Mohamad Armoon
Dec 30 '18 at 4:37
@MohamadArmoon because it isLEFT JOIN, I want to havep.Quantityif theRIGHTtableIS NULL
– Jun Rikson
Dec 30 '18 at 4:40
What is null ?p.LoadingsDetailsorn.Quantityin your LINQ expression ? Share the relevant parts of your entity class definition
– Shyju
Dec 30 '18 at 5:00
@Shyju :p.LoadingsDetails
– Jun Rikson
Dec 30 '18 at 5:01
why don't you check
p.LoadingDetails.First() != null inside Where() ?– Mohamad Armoon
Dec 30 '18 at 4:37
why don't you check
p.LoadingDetails.First() != null inside Where() ?– Mohamad Armoon
Dec 30 '18 at 4:37
@MohamadArmoon because it is
LEFT JOIN, I want to have p.Quantity if the RIGHT table IS NULL– Jun Rikson
Dec 30 '18 at 4:40
@MohamadArmoon because it is
LEFT JOIN, I want to have p.Quantity if the RIGHT table IS NULL– Jun Rikson
Dec 30 '18 at 4:40
What is null ?
p.LoadingsDetails or n.Quantity in your LINQ expression ? Share the relevant parts of your entity class definition– Shyju
Dec 30 '18 at 5:00
What is null ?
p.LoadingsDetails or n.Quantity in your LINQ expression ? Share the relevant parts of your entity class definition– Shyju
Dec 30 '18 at 5:00
@Shyju :
p.LoadingsDetails– Jun Rikson
Dec 30 '18 at 5:01
@Shyju :
p.LoadingsDetails– Jun Rikson
Dec 30 '18 at 5:01
add a comment |
1 Answer
1
active
oldest
votes
You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:
RemainingQuantity = p.LoadingDetails.Any() ?
p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) :
p.Quantity
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975288%2fisnull-in-linq-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:
RemainingQuantity = p.LoadingDetails.Any() ?
p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) :
p.Quantity
add a comment |
You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:
RemainingQuantity = p.LoadingDetails.Any() ?
p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) :
p.Quantity
add a comment |
You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:
RemainingQuantity = p.LoadingDetails.Any() ?
p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) :
p.Quantity
You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:
RemainingQuantity = p.LoadingDetails.Any() ?
p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) :
p.Quantity
answered Dec 30 '18 at 4:57
Ehsan SajjadEhsan Sajjad
50.3k1067123
50.3k1067123
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53975288%2fisnull-in-linq-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
why don't you check
p.LoadingDetails.First() != nullinsideWhere()?– Mohamad Armoon
Dec 30 '18 at 4:37
@MohamadArmoon because it is
LEFT JOIN, I want to havep.Quantityif theRIGHTtableIS NULL– Jun Rikson
Dec 30 '18 at 4:40
What is null ?
p.LoadingsDetailsorn.Quantityin your LINQ expression ? Share the relevant parts of your entity class definition– Shyju
Dec 30 '18 at 5:00
@Shyju :
p.LoadingsDetails– Jun Rikson
Dec 30 '18 at 5:01