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

x
?
Solved

LINQ Sum Decimal GetValueOrDefault Display Number or Empty?

Posted on 2014-07-18
5
Medium Priority
?
751 Views
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)
                              }
                              ).ToList();

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


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

Open in new window

0
Comment
Question by:WorknHardr
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
kumar754 earned 1800 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)
0
 

Author Comment

by:WorknHardr
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:
   No
   Yes / 50:C

public bool IsSubscription { get; set; }

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

Author Comment

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

  Display:
     No
     Yes / $50.00

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

Author Closing Comment

by:WorknHardr
ID: 40210654
Excellent, thx
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

772 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