decimal tmpPreviousSellingRate = db.DataServiceCurrenciesRates
.Where(x => x.CurrencyId == tmpCurrencyId)
.OrderByDescending(x => x.PublicationDate)
.Select(x => x.SellingRate)
.DefaultIfEmpty()
.First();
In db i have:
CurrencyId: 1 PublicationDate: '2014-08-19' SellingRate: 3,4530
CurrencyId: 2 PublicationDate: '2014-08-19' SellingRate: 0,6117
CurrencyId: 3 PublicationDate: '2014-08-19' SellingRate: 1,3570
CurrencyId: 1 PublicationDate: '2014-08-20' SellingRate: 3,3753
CurrencyId: 2 PublicationDate: '2014-08-20' SellingRate: 0,5442
CurrencyId: 3 PublicationDate: '2014-08-20' SellingRate: 1,5478
CurrencyId: 1 PublicationDate: '2014-08-21' SellingRate: 3,38263
CurrencyId: 2 PublicationDate: '2014-08-21' SellingRate: 0,5837
CurrencyId: 3 PublicationDate: '2014-08-21' SellingRate: 1,4635
When CurrentId is 2, I want to get 0,5837, but in tmpPreviousSellingRate is 0,6117 (from 2014-08-19)
I used OrderByDescending and OrderBy - the result is the same, allways 0,6117.
With .Last() instead of .First() I got a error
"Additional information: LINQ to Entities does not recognize the method 'System.Decimal Last[Decimal](System.Linq.IQueryable`1[System.Decimal])' method, and this method cannot be translated into a store expression."