score:2
x.Amount is being converted to a low precision minimum type from "LINQ-to-SQL" conversion, because your collection is IQueryable.
There are several workarounds, the easiest of which is to change the type of your collection to IList, or call ToList() on your collection, forcing the linq query to run as LINQ-to-Objects.
var opasSum = opasForThisIp.ToList().Sum(x => x.Amount);
Note: If you don't want to lose deferred execution by moving away from the IQueryable, you could try casting the Amount to a decimal inside of the linq query.
From MSDN decimal and numeric (Transact-SQL):
In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.
Edit (to include great explanation of different .NET collection types:
Taken from the answer to this SO question.
IQueryable is intended to allow a query provider (for example, an ORM like LINQ to SQL or the Entity Framework) to use the expressions contained in a query to translate the request into another format. In other words, LINQ-to-SQL looks at the properties on the entities that you're using along with the comparisons you're making and actually creates a SQL statement to express (hopefully) an equivalent request.
IEnumerable is more generic than IQueryable (though all instances of IQueryable implement IEnumerable) and only defines a sequence. However, there are extension methods available within the Enumerable class that define some query-type operators on that interface and use ordinary code to evaluate these conditions.
List is just an output format, and while it implements IEnumerable, is not directly related to querying.
In other words, when you're using IQueryable, you're defining and expression that gets translated into something else. Even though you're writing code, that code never gets executed, it only gets inspected and turned into something else, like an actual SQL query. Because of this, only certain things are valid within these expressions. For instance, you cannot call an ordinary function that you define from within these expressions, since LINQ-to-SQL doesn't know how to turn your call into a SQL statement. Most of these restrictions are only evaluated at runtime, unfortunately.
When you use IEnumerable for querying, you're using LINQ-to-Objects, which means you are writing the actual code that is used for evaluating your query or transforming the results, so there are, in general, no restrictions on what you can do. You can call other functions from within these expressions freely.
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Lost decimal precision and scale using LINQ and stored procedure with output parameters
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- How does linq actually execute the code to retrieve data from the data source?
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- How can I combine this code into one or two LINQ queries?
- List of Many-To-Many relationship EF Code-First
- How do I get the files from Directory in order in asp.net?
- Finding an item in a list in c#
- Linq query result into two objects
- Data Field Value Equivalents
- Conditionally Grouping List Items
- Use LINQ to select distinct properties in Lists of Lists
- Understanding lazy evaluation in LINQ in C#
- LINQ to XML via C#
- Schedule optimize
- LINQ: Select all from each group except the first item
- How to fill a list by another list that first list included 2th list in linq?
- LINQ with AND or OR for 2 or more enumerable lists
- NHibernate LINQ queries with nested .Any() conditions
- How to pass textbox from view to it's own controller again and again?
- Select&Distinct into new object with DLinq
- Linq extended set operations
- CopyToDataTable in LINQ
- how can I return a certain amount of items from my db using linq statements in c#
- Logic to decipher consecutive date ranges