使用LINQ

时间:2016-03-11 13:07:30

标签: c# asp.net asp.net-mvc linq

这是我的代码我试图从存储过程中获取Equipment的列表,代码场景与其他代码场景非常不同,因为我无法使其工作。

我已经尝试了许多方法,因为在给定的代码中可以看到但没有任何作用

public ActionResult Test()
{
    var model = new ViewModels()
        {
            ModelEquipmentRequestResult = new List<EquipmentRequest_Result>()
            {
                new EquipmentRequest_Result()
                {
                   EquipmentName = db.EquipmentRequest().ToList().ToString()
          //         (from a in new db.EquipmentRequest

          //select new EquipmentRequest { a.EquipmentName}).ToList();
                   // ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<EquipmentRequest_Result>("EquipmentRequest").ToList().TooString()
                   //db.EquipmentRequest().AsQueryable<EquipmentRequest_Result>(),

                   //custq.AsEnumerable()
                   //   .Select(o => new EquipmentRequest_Result() {
                                      // EquipmentName = o.EquipmentName
                      //}).ToList()

                }
            },
            ModeleEquipmentSpViewModel = new EquipmentSpViewModel()
        };
        ViewBag.project = db.PMSProjects.ToList();

        return View(model);
}

代码行

EquipmentName = db.EquipmentRequest().ToList().ToString()

返回类似这样的内容:

EquipmentName   
System.Collections.Generic.List`1[ProjectManagementSystem.EntityDataModel.EquipmentRequest_Result]

以下是存储过程查询:

SELECT 
    e.EquipmentName, 
    NULL AS Id, 
    NULL AS EquipmentRequestId, 
    NULL AS EquipmentId, 
    NULL AS Planned, 
    NULL AS Actual, 
    NULL AS Remarks 
FROM 
    PECEquipment AS e

以下是我绑定此视图的视图

@model ProjectManagementSystem.ViewModel.ViewModels

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>TestPartail</title>
</head>
<body>
    <div>
        @for (int i = 0; i < Model.ModelEquipmentRequestResult.Count; i++)
        {
            <div>
                @Html.LabelFor(m => m.ModelEquipmentRequestResult[i].EquipmentName)
                @Html.DisplayFor(m => m.ModelEquipmentRequestResult[i].EquipmentName)
            </div>
            <div>
                @Html.LabelFor(m => m.ModelEquipmentRequestResult[i].Planned)
                @Html.EditorFor(m => m.ModelEquipmentRequestResult[i].Planned)
            </div>
    <div>
        @Html.LabelFor(m => m.ModelEquipmentRequestResult[i].Actual)
        @Html.EditorFor(m => m.ModelEquipmentRequestResult[i].Actual)
    </div>
        }
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

澄清:

var model = new ViewModels()
    {
        ModelEquipmentRequestResult = db.EquipmentRequest().Select(result => new EquipmentRequest_Result
        {
               EquipmentName = r.EquipmentName,
               Planned = r.Planned, //replace with DB column name
               Actual = r.Actual //replace with DB column name
        }).ToList(),
        ModeleEquipmentSpViewModel = new EquipmentSpViewModel()
    };

上面的答案也是如此。

答案 1 :(得分:0)

试试这个:

List<EquipmentRequest_Result> output = db
      .EquipmentRequest()
      .Select(x=> new EquipmentRequest_Result { EquipmentName = x.EquipmentName })
      .ToList()

编辑:专门针对此问题的情况

var model = new ViewModels()
            {
                ModelEquipmentRequestResult = db
                    .EquipmentRequest()
                    .Select(x=> new EquipmentRequest_Result { EquipmentName = x.EquipmentName })
                    .ToList(),
                ModeleEquipmentSpViewModel = new EquipmentSpViewModel()
            };
return View(model);