I have simple query from a view in SQL Server:
SELECT [PricePerM]
FROM RealtyStatParent
ORDER BY PricePerM
When I execute the query in SQL Management Studio I get correct results. It means I get 2532 rows starting from 1.00 and ending by 173543.6893.
When I make a query from C# using entity framework, I got the same results:
var justDecimals = context.RealtyStatParents
.OrderBy(item => item.PricePerM)
.Select(item => item.PricePerM)
.ToArray();
Until now nothing special. But what I really don't understand is following query. I select entire rows first and then I select the price (decimal).
var entireRows = context.RealtyStatParents
.OrderBy(item => item.PricePerM)
.ToArray();
var decimalFromRows = entireRows
.Select(item => item.PricePerM)
.ToArray();
A lot of values of PricePerM are repeated (value 1 or 48) instead of a real value and the resultset is not ordered properly.
The definition of row in EF designer is simple:
public partial class RealtyStatParent
{
public Nullable<decimal> PricePerM { get; set; }
public int BusinessCategory { get; set; }
public decimal obec_kod { get; set; }
public Nullable<int> ParentCategoryId { get; set; }
}
UPDATE
I Believe that this strange behavior has something to do with Entity Framework returning bad data, because the view has no primary key. EF decided that Entity Key is on Column BusinessCategory and obec_kod, which is combined unique. I hope I am closer, but still not enough.