I am trying to get the result of CROSS APPLY between a dbSet and a list of object.
public class BudgetRate : BaseEntity
{
public string CurrencyCode { get; set; }
public DateTime ValueDate { get; set; }
public decimal RateValue { get; set; }
public virtual Currency Currency { get; set; }
}
public class TestClass
{
public DateTime ExchangeDate { get; set; }
public string ExchangeCurrencyCode { get; set; }
}
I have tried the following:
List<TestClass> testClasses = new();
// Logic to fill the list
var query = from a in _dbContext.BudgetRate
from b in testClasses
.Where(b => a.ValueDate <= b.ExchangeDate && a.CurrencyCode == b.ExchangeCurrencyCode).ToList()
.OrderByDescending(b => a.ValueDate).Take(1)
select new
{
Key = a.ValueDate.ToString() + ',' + a.CurrencyCode,
a.RateValue,
};
However I am getting the following exception:
The LINQ expression 'a => __testClasses_0
.Where(b => a.ValueDate <= b.ExchangeDate && a.CurrencyCode == b.ExchangeCurrencyCode)
.AsQueryable()
.OrderByDescending(b => a.ValueDate)
.Take(1)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'