score:2
Accepted answer
this will work for you. you can clean it up for your own use, but basically you are just using the %
modulo operator to get the cents and the ? :
ternary operator to select the correct value
var prices = from ps in objectcontext.products
let cents = ps.price % 1
let uptodollar = cents > 0 ? (ps.price + 1 - cents) : ps.price
let uptohalfdollar = cents == 0 ? ps.price : (cents < 0.5 ? (ps.price + 0.5 - cents) : (ps.price + 1 - cents))
select new
{
finalprice = roundingid == 0 ? ps.price : (roundingid == 1 ? uptodollar : uptohalfdollar)
};
score:1
you can do it all inline if you want, but honestly that seems like it'd be hard to read. try this:
int roundingid = 0; //0 = no rounding. 1 = dollar rounding. 2 = half-dollar rounding.
var prices = from ps in objectcontext.products
select new
{
finalprice = getprice((ps.iscustomprice ? ps.customprice : ps.retail), roundingid),
}
private double getprice(double price, int roundingoption)
{
switch (roundingoption)
{
case 0:
//do stuff
break;
case 1:
//do stuff
break;
case 2:
//do stuff
break;
}
}
Source: stackoverflow.com
Related Query
- LINQ to Entities - Rounding to the nearest dollar, nearest half-dollar, or not at all
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method
- LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'
- LINQ to Entities does not recognize the method Int32 get_Item(Int32)
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method
- LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'
- Why LINQ to Entities does not recognize the method 'System.String ToString()?
- LINQ to Entities does not recognize the method 'System.Linq.IQueryable`
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework
- LINQ to Entities does not recognize the method 'System.Object GetValue(...)'
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method ElementAt(i);
- LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method
- LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method,
- LINQ to Entities does not recognize the method 'System.String Format
- The LINQ expression node type 'Lambda' is not supported in LINQ to Entities
- LINQ to Entities does not recognize the method Generic.List(int) to Generic.IEnumerable(int) method
- LINQ to Entities does not recognize the method exception
- LINQ to Entities does not recognize the method 'Method name' method
- LINQ to Entities does not recognize the method 'Int32
More Query from same tag
- Processing lambda query result in C# after performing table join
- Count elements in array of arrays equal to specified value
- Using LINQ GroupBy to group by reference objects instead of value objects
- C# LINQ query creating inefficient SQL compared to original query
- How to create a Predicate<T> dynamically on runtime
- Inner join in collections using LINQ
- How to sum result in each Table and Round from RoundResult in HomePayerResult
- How to Order Data Query in Entity Framework Table Relationships LINQ Method syntax?
- How to get a Header Name from the table at runtime in vb.net
- returning total count of records
- Get the Columns of an Inner Join LINQ query
- How to convert a list of dictionaries into IDictionary “C#”
- complex t-sql to linq query: inner join, group by, select
- LINQ nested list comprehension to get files from several folders
- Fill Datatable from linq query
- Changing CSS of dynamically created controls in ASP.Net
- How to get a distinct list from a list of delimited strings
- LINQ: With Many to many relationship, select from list
- ILookup<TKey, TVal> vs. IGrouping<TKey, TVal>
- Get Linq Expression from FilterQueryOption throw CLR exception
- Joining multidimensional arrays with LINQ on key indices
- Performance issue with Linq query - navigation properties to blame?
- LINQ Sub Query In Result Entity Framework 5
- SQL related , Get latest related record
- How to implement MaxOrDefault(x => x.SomeInt) LINQ extension?
- how to change the type of the parameter in an Expression?
- Count Dates which a specific Customer does not have a Shop in it
- Why EF trim milliseconds in Time and DateTime values
- Problem updating multi-row
- Can someone explain why I'm getting an ambiguous invocation error for this?