How to return my LINQ query result as a particular object
public CountryTable GetSelectedEventInfo(string SelectedEventID)
{
return (CountryTable) this.context.Event.Where(
e => e.EventID.Equals(Convert.ToInt32(SelectedEventID)));
}
Here is my model
public class CountryTable
{
public int EventID { get; set; }
public string Title { get; set; }
public DateTime Startdate { get; set; }
public DateTime EndDate { get; set; }
public string EventUrl { get; set; }
}
Below is my db context
public DbSet<CountryTable> Event { get; set; }
Given below is what I am trying to do
public CountryTable GetSelectedEventInfo(string SelectedEventID)
{
return (CountryTable) this.context.Event.Where(
e => e.EventID.Equals(Convert.ToInt32(SelectedEventID)));
}
I need my query result to return object of type CountryTable
You could use the
SingleOrDefaultmethod:Notice that this method will return null if no matching record is found in your database.