Skip to content

Instantly share code, notes, and snippets.

@venblee
Created June 21, 2013 16:03
Show Gist options
  • Save venblee/5832230 to your computer and use it in GitHub Desktop.
Save venblee/5832230 to your computer and use it in GitHub Desktop.
Select Disctinct using Linq
// Make sure you use DTO and not EF Poco
// Else you will get error
public List<Models.PostalCode> SearchCity(string searchTerm)
{
try
{
using (var db = new PmsContext())
{
var pa = db.PostalCodes.Distinct().Where(x => x.town.Contains(searchTerm)).ToList();
List<Models.PostalCode> cities = (from p in db.PostalCodes
where p.town.Contains(searchTerm)
select new Models.PostalCode()
{
town = p.town,
}
).Distinct().ToList();
// return db.PostalCodes.Where(x => x.town.Contains(searchTerm)).Distinct().ToList();
return cities;
}
}
catch (Exception ex)
{
}
return new List<Models.PostalCode>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment