【发布时间】:2017-06-29 19:41:02
【问题描述】:
我使用此代码从 EntityFrameWork 中的数据库加载数据,但它显示此错误。
无法在 LINQ to Entities 查询中构造实体或复杂类型“DatabaseModel.State”。
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();
【问题讨论】:
-
您已经有一个 State 对象。你为什么要再次创建它?要么按原样返回
states对象Select(state => state),要么完全省略Select语句return db.States.Where(predicate);。您是否尝试省略一些属性?
标签: c# entity-framework linq