Say there are 5 records from the query, how do I get the top 1 records? This is my current code.
public Application GetByUserIdAndVersion(int userId, string version)
{
VettingDataContext dc = new VettingDataContext(_connString);
return (from a in dc.Applications
where a.UserId == userId && a.chr_Version == version
select a).SingleOrDefault<Application>();
}
Just use
FirstOrDefault()instead:SingleOrDefault()will throw an exception if there is more than one record,FirstOrDefault()will just take the first one.Also you shouldn’t have to cast to
Application– your record already is of typeApplication.