![](/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.