I use this code to load data from the database in EntityFrameWork, but it shows me this error.
The entity or complex type 'DatabaseModel.State' cannot be constructed in a LINQ to Entities query.
public class StateRepository : BaseRepository
{
public IQueryable Where(System.Linq.Expressions.Expression<Func<Models.DomainModels.State, bool>> predicate)
{
return db.States
.Where(predicate)
.Select(states => new State
{
Id = states.Id,
Country_Id = states.Country_Id,
Name = states.Name,
PhoneCode = states.PhoneCode
});
}
}
var objStateRepository = new StateRepository();
datagrideview1.DataSource = objStateRepository.Where(p => p.Name.Contains(txtSearchState.Text)).ToList();