I have an exchange rate table. I need to get current rate and previous rate and then compare results.
I can get first record using FirstOrDefault
.
When I am using ElementAtOrDefault
, this error shows "The query operator 'ElementAtOrDefault' is not supported". How can I get the second record?
Select top 2, then select second element.
will fetch the second record in the collection.
There's a detail not explicitly mentioned here:
FirstOrDefault
returns anElement
,Skip(1).Take(1)
returns a set of 1 element; i.e. the type returned bySkip(1).Take(1)
isIEnumerable
, whileFirstOrDefault
is not.If you use
you will get the first and second.
EDIT- If you need both the first and second, then the above will be more efficient than runing your query twice:
As John Anderson already pointed out
Skip(1).Take(1)
returns anIEnumerable
of size 1.In order to get the 2nd entry itself or else a
null
(i.e. no exceptions), I'm using the following:It may not work in your specific case, but may be useful for use cases of other.
Try this simple implementation this might be useful