MVC将行拆分为列或组

问题描述

我想将行分成几列,我有数据:
A-21-PL-21-OFF
A-22-PL-22-OFF
B-1-DE-1-绿色
B-2-DE-2-绿色
C-30-ES-30-橙色
C-31-ES-31-橙色
D-1-TH-1-红色
D-2-TH-2-红色

该数据对我而言显示为列表(所有数据仅一步一步显示在同一列中),我无法按“名称”进行分组,但我想将其分成几列:
Coulmn1:A-21-PL-21-OFF |第2栏:B-1-DE-1-绿色| coulmn3:C-30-ES-30-橙色

这是我生成View的代码,我想在其中获取名称分组数据的列:

        public ActionResult PosList()
    {
        List<StaNowiskoVM> devicesList;

        using (Db db = new Db())
        {

            devicesList = db.StaNowisko.ToArray().Select(x => new StaNowiskoVM(x)).ToList();

        }

        return View(devicesList);
    }

这是我在viewmodel中的变量:

public int Id { get; set; }
public string Name { get; set; }
public string Country{ get; set; }

我从数据库到虚拟机的列表视图:

@foreach (var item in Model) {
<tr>
    <td>
       Name: @Html.displayFor(modelItem => item.Nazwa) + Kraj: @Html.displayFor(modelItem => item.Kraj) + @Html.displayFor(modelItem => item.AktualnyStan)
    </td>
</tr>

现在,当item.nazwa ==“ A” ...

时,我需要在新列上进行拆分

解决方法

您可以尝试 JSX element type 'WrappedComponent' does not have any construct or call signatures.

LINQ
,

这并不是一件容易的事,因为每一列中的项目数可能不同。您需要创建数据透视表。

请参见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] inputs = {
                                  "A - 21 - PL - 21 - OFF","A - 22 - PL - 22 - OFF","B - 1 - DE - 1 - Green","B - 2 - DE - 2 - Green","C - 30 - ES - 30 - Orange","C - 31 - ES - 31 - Orange","D - 1 - TH - 1 - RED","D - 2 - TH - 2 - RED"
                              };
            var uniqueNames = inputs
                .Select(x => new { key = x.Split(new char[] {'-'}).First().Trim(),name = x})
                .GroupBy(x => x.key) 
                .OrderBy(x => x.Key)
                .ToArray();

            DataTable dt = new DataTable();
            foreach (var name in uniqueNames)
            {
                dt.Columns.Add(name.Key,typeof(string));
            }
            int maxRows = uniqueNames.Max(x => x.Count());

            for(int row = 0; row < maxRows; row++)
            {
                DataRow newRow = dt.Rows.Add();
                for(int col = 0; col < uniqueNames.Count(); col++)
                {
                    if(row < uniqueNames[col].Count())
                    {
                        newRow[col] = uniqueNames[col].Skip(row).First().name;
                    }
                }
            }
        }
    }
}