我有一个类,其中包含三个运行良好的方法:
从表中获取数据:
public IList<RetriveOperatorAcYo> GetRetriveOperatorListGrid(int Uid) { var persons = (from p in _Context.People join a in _Context.Accounts on p.PersonId equals a.PersonId join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid select p); return MapToRetriveOperatorYoList(persons); }
用作将列表分隔为单个元素的中间函数:
private IList<RetriveOperatorAcYo> MapToRetriveOperatorYoList(IEnumerable<EntityFramework.Person> persons) { return persons.Select(MapToRetirveOperatorYo).ToList(); }
作为生成结果的映射函数:
private RetriveOperatorAcYo MapToRetirveOperatorYo(EntityFramework.Person person) { var vendorYo = new RetriveOperatorAcYo { Id = person.PersonId, FirstName = person.FirstName, LastName = person.LastName, MobileNumber = person.MobileNumber, LandlineNumber = person.LandlineNumber } return vendorYo; }
现在我想在第一个函数中仅获取特定列的数据。
我搜索了一些技术并尝试了这个:
var persons = (from p in _Context.People
join a in _Context.Accounts on p.PersonId equals a.PersonId
join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId
join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId
where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid
select new
{
PersonId = p.PersonId,
FirstName = p.FirstName,
LastName = p.LastName,
MobileNumber = p.MobileNumber,
LandlineNumber = p.LandlineNumber
});
但它在第二个函数中给了我错误:
"The entity or complex type cannot be constructed in a LINQ to Entities query."
是否有任何解决方案如何映射数据并仅返回具有特定详细信息的特定映射类。
感谢您提前提供的帮助。
最佳答案
要使用 3 个方法维护模式并执行所需的部分查询,同时不在第一个方法中使用现有的 DTO,您将不得不诉诸于传递动态对象并更改方法签名。像这样的东西应该可以工作,尽管不推荐 - 请注意在第二个和第三个方法中使用dynamic
:
public IList<RetriveOperatorAcYo> GetRetriveOperatorListGrid(int Uid)
{
var persons = (from p in _Context.People
join a in _Context.Accounts on p.PersonId equals a.PersonId
join b in _Context.BusinessTypes on a.BusinessTypeId equals b.BusinessTypeId
join d in _Context.AccountUserRelations on a.AccountId equals d.AccountId
where b.Name == AccountBusinessTypes.Operator && a.IsDelete == false && d.Active == true && d.UserId == Uid
select new
{
PersonId = p.PersonId,
FirstName = p.FirstName,
LastName = p.LastName,
MobileNumber = p.MobileNumber,
LandlineNumber = p.LandlineNumber
});
return MapToRetirveOperatorYo(persons);
}
private IList<RetriveOperatorAcYo> MapToRetriveOperatorYoList(IEnumerable<dynamic> persons)
{
return persons.Select(MapToRetirveOperatorYo).ToList();
}
private RetriveOperatorAcYo MapToRetirveOperatorYo(dynamic person)
{
var vendorYo = new RetriveOperatorAcYo
{
Id = person.PersonId,
FirstName = person.FirstName,
LastName = person.LastName,
MobileNumber = person.MobileNumber,
LandlineNumber = person.LandlineNumber
}
return vendorYo;
}
更好的选择是将您的第一个方法更改为select new RetriveOperatorAcYo
,如 this answer 中所述。 .
关于c# - 在映射中使用 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755972/