ASP.NET - Get Single value! - Asked By myzonal.com myzonal.com on 27-Jun-12 05:22 AM

How we get single value from database through Lamda Expression or linq..........
Chintan Vaghela replied to myzonal.com myzonal.com on 27-Jun-12 05:25 AM

Hi Frndz,

 

Functionality:  Gettting Single value using LINQ

 

 

To achieve this task,

 

Use SingleOrDefault Function

 

 

 

 

 

Make your Logic as following way

 

Logic ::

 

  DataClassesDataContext db = new DataClassesDataContext();

 

        var obj = (from tbl in db.tbl_empdetails

                 select tbl).SingleOrDefault();

 

        string empname = obj.empname;

 

Hope this helpful!

Thanks

 

 

 

myzonal.com myzonal.com replied to Chintan Vaghela on 27-Jun-12 05:31 AM
what name space would be used  for "DataClassesDataContext" ?
Jitendra Faye replied to myzonal.com myzonal.com on 27-Jun-12 05:40 AM
Use this code-

DataClassesDataContext db = new DataClassesDataContext();


      var obj = (from data in db.tablename

         select data.fieldname).SingleOrDefault();

 


 
Chintan Vaghela replied to myzonal.com myzonal.com on 27-Jun-12 05:42 AM
DataClassesDataContext  is DBML class
Ravi Maurya replied to myzonal.com myzonal.com on 27-Jun-12 05:47 AM
You can use FirstOrDefault function like this

In Linq:

(from p in context.EventMasters
where p.EventId==5
select p).FirstOrDefault()

In Lambda Exp:

context.EventMasters.Where(e=>e.EventId==5).Select(p => p).FirstOrDefault()

Hope this might help you.
RAJASEKHAR RAJENDRAN replied to myzonal.com myzonal.com on 27-Jun-12 07:29 AM
Hi,

You can achieve this in LINQ using the below .

try
{
return database.Employees.SingleOrDefault(x => id.Equals(x.ID));
}
catch (InvalidOperationException Ix)
{
  throw;
}
if you use SingleOrDeault() as above it will return null if there are no results. it will throw exception if encountered multiple results, you could also use FirstOrDefault to avoid such a Exception occurring.

Thanks & Regards,
Rajasekhar.R

TSN ... replied to myzonal.com myzonal.com on 27-Jun-12 08:08 AM
hi..


to get a single value from a collection using Lambda or linq you can use the Following functions.

.Take(1); // Takes the Single value from Collection , it is the basically the first value.
.Single(); // gets you the single value but raises an error if the result is more than one value.
.SingleorDefault(); // same as above Explained but additonally it works for null values also
.First() // Takes the First Record fromthe result set.
.FirstOrDefault() ; // Same as Above but additonally it works for null values also

you can use any one of the Above methods to take single value from the Collection
[)ia6l0 iii replied to myzonal.com myzonal.com on 27-Jun-12 09:28 PM
Post your linq query or the current lambda expression you have. We will tweak that and give you a solution. 
Otherwise, you will only get replies like below.

A Problem well stated is half solved.
[)ia6l0 iii replied to myzonal.com myzonal.com on 27-Jun-12 09:28 PM
Post your linq query or the current lambda expression you have. We will tweak that and give you a solution. 
Otherwise, you will only get replies like above.

A Problem well stated is half solved.