LINQ Sum Decimal GetValueOrDefault Display Number or Empty?

Posted on 2014-07-18
Last Modified: 2016-02-15
I have a MVC 5 web view which correctly displays :  Yes / $50.00

I need to display nothing if the database value is null or 0 like so;  No

Instead it currently displays:  No $0.00

Here is my current working LINQ:

 var model =  (from d in context.Donations.ToList()
                              group d by new { d.From_Id } into g
                              select new SubscriptionViewModel
                                  IsSubscription = g.Select(s=> (bool)s.IsSubscription ? "Yes / " : "No ").FirstOrDefault(),
                                  [u]AmountSub[/u] = g.Sum(s => (decimal?)s.AmountSub).GetValueOrDefault(0),
                                  Donated = g.Sum(t => (decimal?)t.Amount).GetValueOrDefault(0)

Open in new window

View Model
    public class DonorsViewModel
        public string IsSubscription { get; set; } 

        [DisplayFormat(DataFormatString = "{0:c}")]
        public decimal [u]AmountSub [/u]{ get; set; }

        [DisplayFormat(DataFormatString = "{0:c}")]
        public decimal Donated { get; set; }  

Open in new window

        @Html.DisplayFor(m =>@item.IsSubscription) @Html.DisplayFor(m =>@item.[u]AmountSub[/u])

Open in new window

Question by:WorknHardr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 12

Accepted Solution

kumar754 earned 450 total points
ID: 40205906
// leave the amount, you might need this field for calculation, rather create a read only property in your ViewModel and format the display using existing properties:

[View Model]
    public class DonorsViewModel
        public string IsSubscription { get; set; }

        [DisplayFormat(DataFormatString = "{0:c}")]
        public decimal AmountSub { get; set; }

        [DisplayFormat(DataFormatString = "{0:c}")]
        public decimal Donated { get; set; }  

        public string SubscriptionAmount {
            get { return (IsSubscription == "Yes") ? string.Format("Yes / {0}:C", this.AmountSub) : "No"; }

and then use in your UI:

 @Html.DisplayFor(m =>@item.SubscriptionAmount)

Author Comment

ID: 40206624
Okay, I get the idea of moving the formatting into the View Model, so I change 'IsSubscription' back to bool like it should be and changed your code very slightly. It now displays like this:
   Yes / 50:C

public bool IsSubscription { get; set; }

get { return (IsSubscription == true) ? string.Format("Yes / {0}:C", this.AmountSub) : "No"; }

Author Comment

ID: 40206632
Got it, changed formatting like so:
     "Yes / " + string.Format("{0:c}", this.AmountSub)

     Yes / $50.00

Q. Why dosen't the DisplayFormat data annotation above AmountSub work if code is like this: "Yes / " + this.AmountSub) ?

Author Closing Comment

ID: 40210654
Excellent, thx

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

Suggested Solutions

Title # Comments Views Activity
Web page design problem 3 41
AJAX Create / Update Not working MVC 5 63
Removing Version of .NET in Windows 10 17 122
async questions 5 48
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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