Unable to pass an object of type "System.Collections.Generic.List`1 [System.Decimal]" to enter "System.IConvertible"

var itemlist = (from u in db.getitems select u).ToList(); var item= new EstimatesModel { id = Convert.ToInt64(estimatelist.id), expiry_date = estimatelist.expiry_date, terms_conditions = estimatelist.terms_conditions, rate = Convert.ToDecimal(itemlist.Select(m =>m.taxrate).ToList()) }; return View(item); 

Here in this request, note that the rate has more than 1 elements coming from getitems , where rate is Decimal , so I converted it.

Then I return the idea of ​​a single item object, where only the rate column is List .

But I get this error while debugging -

Cannot pass an object of type "System.Collections.Generic.List`1 [System.Decimal]" to enter "System.IConvertible".

I will use this as on the watch page -

 @for (int i = 0; i < Model.rate; i++) { <ul> <li><a href="#">@i</a></li> </ul> } 
+6
source share
3 answers

You create an IEnumerable<decimal> with this part of the line that assigns a value to the speed

  itemlist.Select(m =>m.taxrate) 

You are now materializing IEnumerable in a List<decimal> with

  itemlist.Select(m =>m.taxrate).ToList() 

and pass this list to Convert.ToDecimal , and as far as I know, the Convert.ToDecimal overload that accepts the list does not exist. So the error.

To solve your problem, we need to know what the EstimatesModel.rate type is.

If it is a simple decimal (not a list of decimals), then you need to specify which value should be used from the whole list. First, last, amount, average?

for instance

 rate = itemlist.Max(m =>m.taxrate); 

or

 rate = itemlist.First().taxRate; 

EDIT Following the comment below, if you want to store in the EstimatesModel class a list of all bets returned by db.getitems , you need to define the rate field as List<decimal>

 public class EstimatesModel { .... List<decimal> rate; } 

and then you can just create your list with

 rate = itemlist.Select(m =>m.taxrate).ToList() 

no need Convert.ToDecimal(... m.taxrate ...) be taxrate already decimal

+7
source

rate = Convert.ToDecimal(itemlist.Select(m =>m.taxrate).ToList()) - problem. Does the scalar decimal speed, if so, do you want the value max / min / avg? For the maximum attempt:

rate = Convert.ToDecimal(itemlist.Max(m =>m.taxrate))

If you need to convert each item to an itemlist and return a list, try:

rate = itemlist.Select(m =>Convert.ToDecimal(m.taxrate)).ToList()

Assuming the speed is List<decimal>

+2
source

This is the correct way to convert all elements to a list in decimal.

 rate = itemlist.Select(x => Convert.ToDecimal(x.taxrate)).ToList(); 
0
source

All Articles