Solved

LINQ Sum Decimal GetValueOrDefault Display Number or Empty?

Posted on 2014-07-18
5
615 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

23 Experts available now in Live!

Get 1:1 Help Now