[英]Convert list object to IEnumerable<> in ASP.NET Core
I built a join query using EF in the controller side.我在控制器端使用 EF 构建了一个连接查询。
How can I pass the columns that I need from the controller to the view?如何将我需要的列从控制器传递到视图?
My query is :我的查询是:
var unretObj = _unitOfWork.CartsTransaction.GetUnreturnedCarts(branchId, compId)
.Join(_unitOfWork.ApplicationUser.GetAll(),
ct => ct.UserId,
au => au.UserId,
(ct, au) => new { ct, au })
.Join(_unitOfWork.Lock.GetAll(),
ctau => ctau.ct.CartMacId,
l => l.MacId,
(ctau, l) => new { ctau,l })
.Select(result => new
{
result.ctau.ct.UnlockTime,
result.l.LockIdByComp,
result.ctau.au.FullName,
result.ctau.au.PhoneNumber
}).ToList();
I created a model class (not mapped to the database) with the columns that I need:我创建了一个模型类(未映射到数据库),其中包含我需要的列:
[NotMapped]
public class UnreturnedLocks
{
public DateTime UnlockTime { get; set; }
public int LockNumber { get; set; }
public string FullName { get; set; }
public string PhoneNumber { get; set; }
}
How can I convert the object unretObj
to an IEnumerable<UnreturnedLocks>
so that I can pass it to the view ?如何将对象
unretObj
转换为IEnumerable<UnreturnedLocks>
以便将其传递给视图?
Or is there another way to use the general object in the view without converting it?还是有另一种方法可以在视图中使用通用对象而不转换它?
.Select(result => new {...}
will return an IQueriable
of anonymous objects, you then need to convert them to IEnumerable<UnreturnedLocks>
like this: .Select(result => new {...}
将返回匿名对象的IQueriable
,然后您需要将它们转换为IEnumerable<UnreturnedLocks>
,如下所示:
...
.Select(result => new
{
result.ctau.ct.UnlockTime,
result.l.LockIdByComp,
result.ctau.au.FullName,
result.ctau.au.PhoneNumber
})
.AsEnumerable()
.Select(res => new UnreturnedLocks
{
UnlockTime = res.UnlockTime
....
})
.ToList();
Alternatively, you can useAutomapper Queryable Extensions或者,您可以使用Automapper 可查询扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.