簡體   English   中英

如何使用LINQ獲取特定類別的項目列表?

[英]How to get the list of items of a particular class using LINQ?

我正在開發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)  

在此處輸入圖片說明

既然看來,一旦您可以手動創建tableGrid不允許再添加相同的屬性名稱:

<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

我將首先獲取所有可能的位置名稱,然后為它們中的每個創建一個列:

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名稱列表的情況下起作用,否則將引發異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM