![](/img/trans.png)
[英]What is the most elegant way to get a set of items by index from a collection?
[英]Get and set items in a collection
我有兩個班級:組和項目。
public class Group
{
public string Name{ get; set; }
public List<Item> ItemList { get; set; }
}
然后項目
public class Item
{
public int ID{ get; set; }
public string Name{ get; set; }
public string Description{ get; set; }
public Group ItemGroup{ get; set; }
}
每個小組表演都有一組項目。
以下代碼旨在獲取特定組的項目列表。
public IEnumerable<Item> GetItemByGroup(string group)
{
return repository.GetAllItems().Where
(p =>p.ItemGroup.Name.Equals(group));
}
完整的控制器代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TestService.Models;
using System.IO;
namespace TestService.Controllers
{
public class ItemsController : ApiController
{
static readonly IItemRepository repository = new ItemRepository();
public IEnumerable<Item> GetAllItems()
{
return repository.GetAllItems();
}
public IEnumerable<Item> GetItemsByGroup(string group)
{
return repository.GetAllItems().Where
(p => p.Category.Name.Equals(group));
}
}
}
當我運行此命令時,出現錯誤: No type was found that matches the controller named 'item'.
當我使用localhost:1234/api/item?group=group1
調用它時。
如何獲取字符串指定的特定類別中的項目列表?
您的網址不正確。 嘗試... / api / 項目 ...而不是... / api / 項目 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.