简体   繁体   中英

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.

您必须使用orderby decending和Take(1),因为Linq无法将Last()转换为任何有效的sql语句。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM