简体   繁体   English

在 ASP.NET Core 中将列表对象转换为 IEnumerable<>

[英]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.

相关问题 将数据表转换为 IEnumerable<T> 在 ASP.NET Core 2.0 中 - Convert DataTable to IEnumerable<T> in ASP.NET Core 2.0 在 ASP.NET MVC 视图中转换 IEnumerable(到数组、列表) - Convert IEnumerable (to array, list) in ASP.NET MVC View 在ASP.NET Core中流式处理IEnumerable - Streaming and disposing an IEnumerable in ASP.NET core 如何在 asp.net 核心 mvc ZD7EFA19FBE7D39372FD5ADB60242 中将列表 object 转换为 Viewmodel object? - How can I convert List object to Viewmodel object in asp.net core mvc C#? 为什么 ASP.NET Core 操作可以返回 `ActionResult <IEnumerable<ZapResult> &gt;` 返回任何对象的 `BadRequest()`? - Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object? Angular 8 post formData到ASP.NET Core API无法绑定IEnumerable / List - Angular 8 post formData to ASP.NET Core API cannot bind IEnumerable/List ASP.NET 核心 MVC 后 model 与 IEnumerable 或列表字段是 null - ASP.NET Core MVC post model with IEnumerable or List fields are null ASP.NET 内核 — 上传.json 文件并转换为列表<t></t> - ASP.NET Core — Upload .json file and convert to List<T> 如何在 ASP.NET Core MVC 中进行 IEnumerable 查询? - How to IEnumerable query in ASP.NET Core MVC? ASP.NET Core-绑定IEnumerable <T> 到POST上的ViewModel字段 - ASP.NET Core - Bind IEnumerable<T> to a ViewModel field on POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM