how to get second record in linq to sql

2020-02-09 02:46发布

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?

9条回答
Viruses.
2楼-- · 2020-02-09 03:25

Use .Skip(1) method

查看更多
不美不萌又怎样
3楼-- · 2020-02-09 03:27

Take() returns null if the element is not there (so is equivalent to FirstOrDefault()).

If you'd rather an exception was thrown (cos the second element is not there) like First() then use:

Skip(1).Single()
查看更多
霸刀☆藐视天下
4楼-- · 2020-02-09 03:34

You can try this:

var query=data.Skip(1).Take(1);
查看更多
登录 后发表回答