SantyEssac
SantyEssac

Reputation: 809

Retrieving data from the table using LINQ to SQL

I want to know all the Select Statement that is used to retrieve the data of one particular table from the database. I have used .First and .FirstOrDefault it gives me the desired result but when i use .Last it throws me an error.My feeling is that .First gives us the first row of the table so why error in .Last and why we use .FirstOrDefault because .First will surely give us First row so why to use .FirstOrDefault and what is the default value of it as it is used to be away from the any exception and please let me know the other functions as well to retrieve the data from the table. I have used

var val=db.AccountBank .First();//gives me the Result
var val1=db.AccountBank .Last();//gives me the following error

LINQ to Entities does not recognize the method 'Invoice.AccountBank LastAccountBank' method, and this method cannot be translated into a store expression.

Upvotes: 1

Views: 1037

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222532

You have to use orderby decending and Take(1) because Linq can not translate the Last() to any valid sql statment.

AccountBank.OrderByDescending().Take(1).SingleOrDefault();

Upvotes: 1

Related Questions