Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ Sum Decimal GetValueOrDefault Display Number or Empty?

Posted on 2014-07-18
5
Medium Priority
?
719 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
[X]
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
5 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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