filtering the results

Posted by Santoshkumar under ASP.NET on 4/14/2011 | Points: 10 | Views : 1024 | Status : [Member] | Replies : 4
I wrote the query to get data from database. for example select * from employee. It will get all details of the employees.
so I want to display only one record in from view. I wrote select * from employee. when i bind the data to formview. I want to display only first record.




Responses

Posted by: Ndebata on: 4/14/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Santosh,

You can use LINQ to get any record form the list.

Let say your employee list is lstEmp
List<Employee> lstEmp=new List<Employee>();


To get the first element of collection you can use
lstEmp.FirstOrDefault();

The return type in above case is Employee.
Default Value is NULL

in your case as you need a collecction to bind you can use
lstEmp.Take(1) ;

The above statement takes the first element of the collection.

Lets say you want to get the 5th record in the list,
you can very well use
lstEmp.Skip(4).Take(1) ;



Thanks,
Debata

Santoshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sutotpal on: 4/15/2011 [Member] Starter | Points: 25

Up
0
Down
hi,

i think, you should write this query

select  Top 1 * from employee


It will return, only one & latest record.

Best Regards,
Sutotpal

Santoshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 4/18/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
hi...

try this


select Top 1 * from employee DESC

Best Regard's
Prabhakar

Santoshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bugwee on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down
hi Santoshkumar,

If you wanted to display only one record and not to display all employee records then you should not use Select * from employee. Only select select Top 1 * from employee to minimize query time.

Regards,
Bugwee

Santoshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response