Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i created this simple function to convert Decimal to user decimal fraction settings :
public static decimal GetDecimalParts(this decimal value, string CurrencyPart = ".")
        {
            for (int i = 0; i < BasicVars.CurrencyPart; ++i)
            {
                CurrencyPart += "0";
            }
            decimal ReturnedValue = Convert.ToDecimal(value.ToString(CurrencyPart));
            return ReturnedValue;
        }


how to use it in entity framework query ?

What I have tried:

i tried that : MaxPrice = Convert.ToDecimal(s.MaxPrice).GetDecimalParts()

var data = DB1.view_items_stocks_ranges_prices_noserials.Select(s=> new
            {
                s.DetunitID,
                MaxPrice = Convert.ToDecimal(s.MaxPrice).GetDecimalParts(),
                s.MinPrice,
                s.NOSStoreID,
                s.percent,
                s.stitems_Code,
                s.stitems_ID,
                s.stitems_Name,
                s.stitems_Status,
                s.stitems_Type,
                s.Stock,
                s.UnitName
            }).OrderByDescending(u => u.percent).Where(u => u.stitems_Status == 1);


but this error appear :
Severity Code Description Project File Line Suppression State

Error CS0854 An expression tree may not contain a call or invocation that uses optional arguments
Posted
Updated 24-Sep-19 0:02am

Try and create an extension that omits the optional param but functions the same way

public static decimal GetDecimalParts(this decimal value, string CurrencyPart)
        {
            for (int i = 0; i < BasicVars.CurrencyPart; ++i)
            {
                CurrencyPart += "0";
            }
            decimal ReturnedValue = Convert.ToDecimal(value.ToString(CurrencyPart));
            return ReturnedValue;
        }

public static decimal GetDecimalParts(this decimal value)
        {
            return GetDecimalParts(value, ".");
        }
 
Share this answer
 
Comments
BillWoodruff 24-May-19 4:58am    
+5
You have to explicitly provide the default value of the parameter in the lambda expression.

C#
var data = DB1.view_items_stocks_ranges_prices_noserials.Select(s=> new
            {
                s.DetunitID,
                MaxPrice = Convert.ToDecimal(s.MaxPrice).GetDecimalParts("."),
                s.MinPrice,
                s.NOSStoreID,
                s.percent,
                s.stitems_Code,
                s.stitems_ID,
                s.stitems_Name,
                s.stitems_Status,
                s.stitems_Type,
                s.Stock,
                s.UnitName
            }).OrderByDescending(u => u.percent).Where(u => u.stitems_Status == 1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900