Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get second record in linq to sql

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?

like image 329
ebattulga Avatar asked May 05 '09 12:05

ebattulga


People also ask

How do I get the second last record in LINQ?

var result = ls. Reverse(). Skip(1). Take(1);

How do I get the LINQ query in SQL?

You can get same generated SQL query manually by calling ToString: string sql = committeeMember. ToString(); This overridden method internally calls ObjectQuery.

Is LINQ converted to SQL?

LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a component of version 3.5 of the . NET Framework and ably does the translation of language-integrated queries of the object model into SQL. These queries are then sent to the database for the purpose of execution.

Why LINQ is faster than SQL?

The popular answer is that LINQ is INtegrated with C# (or VB), thereby eliminating the impedance mismatch between programming languages and databases, as well as providing a single querying interface for a multitude of data sources.


2 Answers

You can try this:

var query=data.Skip(1).Take(1);
like image 194
Valentin V Avatar answered Oct 07 '22 17:10

Valentin V


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()
like image 44
bytedev Avatar answered Oct 07 '22 17:10

bytedev