Solved

LINQ Sum Decimal GetValueOrDefault Display Number or Empty?

Posted on 2014-07-18
5
596 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
5 Comments
 
LVL 12

Accepted Solution

by:
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)
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now