how to return the query result as a specific object type in LINQ

c# entity-framework linq linq-to-sql return-type

Question

Sorry I'm new to LINQ and Entity framework.

When a linq query fetches a whole Entity, how can i return the result as an object type? I mean not just a field but also to return all the fields of that specific record, as one entity.

For example a Staff table in sql. To return a staff object entity.

if i write Linq code like this:

var db= (from c in context.Staff
                          where c.id == sId
                          select new { s.sId,s.staffNo,s.name,s.surname,s.image});
return db; ---> here 

Gives an error because i want to return result as Staff object.

How can it return as a table column object?

Thank you

1
0
7/11/2014 6:22:45 PM

Accepted Answer

As per your comments, you need a single object returned from your method.

To return a single Item you have following options.

  1. First: Returns first item from the collection, if there are no matching items then an exception would be thrown.
  2. FirstOrDefault: Works same like First except it would return null if there are no matching records.
  3. Single: Returns only one item from the list, It will throw exception if the collection contains more than one item or the collection is empty. A possible usage is when querying against primary key. Only one record should be returned. Exceptional case should be when there are more than one record.
  4. SingleOrDefault: Works on the same principal as Single except it would return null if the collection is empty.

All these methods can be added at the end of your query syntax or could receive a predicate. Like:

var db=  (from c in context.Staff
         where c.id == sId
         select c).FirstOrDefault();

Or

var db = context.Staff.FirstOrDefault(c=> c.id == sId);

If your field id is a primary key then use SingleOrDefault.


(Old Answer

If you are trying to return particular columns from a table, then you can't project your result to your class Staff since that is generated through the framework.

You have two options. First create another class with properties same as your result set and return its object.

Or instead of selecting specific columns, select all columns and then return Staff type object.

var db=  from c in context.Staff
         where c.id == sId
         select c;
return db;

If you want to create an other class lets say PartialStaff then you can do:

var db=  from c in context.Staff
         where c.id == sId
         select new  PartialStaff
             { 
               Id = s.sId,
               //.......rest of the properties
             };

But then your method return type should be PartialStaff type collection.

4
7/14/2014 12:15:35 PM

Popular Answer

When a linq query fetches a whole Entity, how can i return the result as an object type? I mean not just a field but also to return all the fields of that specific column, as one entity.

For example a Staff table in sql. To return a staff object entity.

You could try this one:

var db= (from c in context.Staff
         where c.id == sId
         select c);

or

db = context.Staff.Where(s=>s.id == sId);

The result of the above query is a sequence of Staff objects, IEnumerable<Staff>. Specifically, this sequence will contain all the Staff objects in context.Staff, whose id is equal to sId.

Gives an error because i want to return result as Staff object.

Now If you are sure that there would be at most one item in context.Staff with the given id, you could try this:

 var db = (from c in context.Staff
         where c.id == sId
         select c).SingleOrDefault();

or

var db = context.Staff.SingleOrDefault(s=>s.id == sId);

This query will return the single Staff object with the given id and if there isn't any Staff object in context.Staff with this id, will return null.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow