如何使用LINQ获取特定类的项目列表?

时间:2014-04-05 10:27:34

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

我正在开发一个MVC应用程序。

我想在View中显示perticuler对象的Items列表。

在下面的类中,InventoryItem列表是产品的一部分,可以包含n个项目。

我在模型中有以下课程。

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<InventoryItem> Inventory { get; set; }
}


public class InventoryItem
{
    public int Id { get; set; }
    public Product Product { get; set; }
    public Location Location { get; set; }
    public int Quantity { get; set; }
}

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }

}

现在我创建了一个带有四个库存物品的产品......

 public List<Product> GetAllProductList()
        {

            Product oProduct = new Product();

            oProduct = oProdRepo.GetProductById(1);

            oProduct.Inventory = new List<InventoryItem>
            {
                new InventoryItem { Location = oLocationRepo.GetLocationById(1),Quantity = 234,Product = oProduct},
                new InventoryItem { Location = oLocationRepo.GetLocationById(2),Quantity = 123,Product = oProduct},
                new InventoryItem { Location = oLocationRepo.GetLocationById(3),Quantity = 642,Product = oProduct},
                new InventoryItem { Location = oLocationRepo.GetLocationById(4),Quantity = 534,Product = oProduct}

            };

            InventoryProductList.Add(oProduct);

        return InventoryProductList;

    }

上述方法创建具有4个库存物料的产品。它的工作完美。

现在我想在网格列中显示上面的库存项目,即4。

我被困在如何显示这些InventoryItems的Grid列?

我有这个视图代码....

@model IEnumerable<StockWatchServices.DomainClass.Product>
     @Html.Grid(Model).Columns(columns => 
                        {
                            columns.Add(c => c.Name).Titled("Product Name").SetWidth(175).Sortable(true).Filterable(true);                      
                       1->       columns.Add(c => c.Inventory.Where(r=>r.Location.Nam ????? ) What to write here ? 
                        2 Column->  ? 
                        3 Column->  ? 
                        4 Column->  ?  

                        }).WithPaging(5)  

enter image description here

2 个答案:

答案 0 :(得分:1)

一旦你可以手动创建Gridtable似乎不允许更多地添加相同的属性名称:

<table>
    <thead>
        <tr>
            @{
                var locationNames = Model.SelectMany(n => n.Inventory).Select(n => n.Location.Name).Distinct().ToArray();
                foreach (var locationName in locationNames)
                {
                    <td>@locationName</td>
                }
            }
        </tr>
    </thead>
    <tbody>
        @foreach(var product in Model)
        {
            <tr>
                @foreach(var columnName in locationNames)
                {
                    <td>@product.Inventory.Single(i => i.Location.Name == columnName).Quantity</td>
                }
            </tr>
        }
    </tbody>
</table>

并将一些CSS应用于

答案 1 :(得分:0)

我首先获取所有可能的位置名称,然后为每个位置名称创建一个列:

var locationNames = Model.SelectMany(n => n.Inventory).Select(n => n.Location.Name).Distinct().ToArray();
foreach(var locationName in locationNames)
{
    var columnName = locationName;
    columns.Add(m => m.Inventory.Single(n => n.Location.Name == columnName).Quantity, columnName).Titled(columnName);
}

修改 我已经下载了相同的Grid.MVC NuGet包,您需要使用接受Columns.Add参数的columnName重载,否则会抱怨列重复

警告是假设所有Product将具有相同的Location名称列表,否则会抛出异常