IsNull in LINQ Sum()












0















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'




enter image description here










share|improve this question

























  • 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











  • 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
















0















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'




enter image description here










share|improve this question

























  • 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











  • 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














0












0








0








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'




enter image description here










share|improve this question
















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'




enter image description here







c# sql-server entity-framework linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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



















  • 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











  • 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

















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












1 Answer
1






active

oldest

votes


















2














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





share|improve this answer























    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%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









    2














    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





    share|improve this answer




























      2














      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





      share|improve this answer


























        2












        2








        2







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 4:57









        Ehsan SajjadEhsan Sajjad

        50.3k1067123




        50.3k1067123






























            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%2f53975288%2fisnull-in-linq-sum%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'