Here is the method:
public IEnumerable<???> GetAllSiteVisits()
{
var visits =
_db.STEWARDSHIP
.OrderByDescending(r => r.VISIT_DATE)
.Select(r => new
{
id = r.STEWARDSHIP_ID,
name = r.SITE.SITE_NAME,
visit_date = r.VISIT_DATE,
visit_type = r.VISIT_TYPE_VAL.VISIT_TYPE_DESC
});
return visits;
}
I am using the Entity Framework 4.2.
So the problem is figuring out the return type. If I were just to use the info directly from the DB table, I’d return IEnumerable<STEWARDSHIP> but since I am selecting only certain fields, I don’t understand what the return type is.
Return type will be a collection of
AnonymousTypewhere each item represents a class with the following properties:id(with type of the r.STEWARDSHIP_ID)name(with type of the r.SITE.SITE_NAME)visit_date(with type of the r.VISIT_DATE)visit_type(with type of the r.VISIT_TYPE_VAL.VISIT_TYPE_DESC)Then you can loop through the elements using
foreachloop:If you need to return results of the query from the method you can introduce a new class which represent these fore properties:
And update query to create instances of VisitDetails rather than Anonymous Type:
In this way return type would be
IEnumerable<VisitDetails>so you easily can even return results of the query itself from the method by specifying exact return type as well so code would be much clean.See Object Initializers with anonymous types for more details.