public static EntityCollectionBase<CurrencyEntity> CurrencyFetch(IPredicateExpression filter, ISortExpression sort)
{
EntityView<CurrencyEntity> jurisTypeView = new EntityView<CurrencyEntity>(GetCurrencies(), filter, sort);
EntityCollectionBase<CurrencyEntity> entities = jurisTypeView.ToEntityCollection();
return entities;
}
In above code you are passing the filter to a EntityView, which means that you are applying the filter in-memory and that is what the exception shows. Is that what you really want (filter in memory and not in database)? Please read that link to know what I'm talking about.
Due to the EntityView, a PostCollectionChangeAction is applied when you tried to access the Count (I guess). If you really want to filter in-memory and avoid that error, you could set the PostCollectionChangeAction.NoAction at the constructor:
EntityView<CurrencyEntity> jurisTypeView = new EntityView<CurrencyEntity>(GetCurrencies(), filter, sort, PostCollectionChangeAction.NoAction);
If you accidentally are using EntityView, just simply your code:
public static CurrencyEntity GetCurrency(string currencyCode)
{
IPredicateExpression filter = new PredicateExpression(CurrencyFields.CurrencyCode == currencyCode);
CurrencyCollection entities = GetCurrencies(filter);
if (entities != null && entities.Count > 0) // throws NullReferenceException
{
return entities[0];
}
else
{
return null;
}
}
public static CurrencyCollection GetCurrencies(IPredicateExpression filter)
{
if (currencies == null)
{
currencies = new CurrencyCollection();
SortExpression sort = new SortExpression(CurrencyFields.CurrencyCode | SortOperator.Ascending);
currencies.GetMulti(filter, 0, sort);
}
return currencies;
}