How to convert a string to decimal within an IQueryable

c# entity-framework-6 iqueryable

Accepted Answer

What you are attempting to accomplish is not supported, as has previously been stated.

  • The best course of action is to modify the database field type to reflect what it really means: Money/Decimal
  • If that's not feasible, speak with the person who can make it happen.

If it is not feasible, the following would be the closest solution:

public class PriceList
{
    public string Price { get; set; }
    public decimal ParsedPrice => decimal.Parse(Price);
}

After that:

.Select(d=>new PriceList
{
    Price = d.Price
});

On the other hand, you cannot run any DbOperations onParsedPrice then. Also take notice that with EF6, a function Object() { [native code] } with arguments cannot be used. The following issue you would run into is that.

For CRUD, EF performs better. For further information, you should probably have a service that gathers the data you want, materialises it, and creates a DTO or business object from it.

comparable to that

public class MyService
{
    public IEnumerable<PriceList> GetPriceList(Expression<Func<MyEntity, bool>> predicate)
    {
        var data = _context.MyEntity.Where(predicate).ToList();
        foreach (var item in data)
        {
            var dto = new PriceList {...}
            yield return dto;
        }        
    }
}
1
12/7/2017 5:17:53 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow