iCosmin iCosmin - 3 years ago 238
C# Question

WPF LINQ query result as percentage

How can I concatenate the '%' symbol at the end of a query result? I know how to do it in SQL but I have no idea how to do it with linq. The result that I am getting now is 83. How can I make it 83%?

private void SetLaborStandard()
int.TryParse(MDate.ToString("MM"), out int month);
LaborStandard = Decimal.Round(Convert.ToDecimal(db.KPIs.Where(a => a.sMonth == month).Select(a => a.LaborStandard*100).FirstOrDefault()), 2, MidpointRounding.AwayFromZero);

Answer Source

Since you have marked this post with WPF and MVVM, I thought I'd share with you that this kind of formatting can be handled directly by XAML. You do not need to append % sign with the value. Just expose your property as standard Decimal type and bind your UI control (TextBlock, Label or whatever) to this property. Within your binding, you should use StringFormat to specify that this value should be formatted as percentage like this:

<TextBlock Text="{Binding Path=LaborStandard, StringFormat={}{0}%}" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download