는 제가 DB에서 배열 제품을 채우기 위해 원하는 컨트롤러를리스트에서 배열을 채우는 방법 <T>?
namespace WebForms
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return products.Where(
(p) => string.Equals(p.Category, category,
StringComparison.OrdinalIgnoreCase));
}
}
}
있습니다.
나는 클래스 제품이 있습니다. 내 코드에서 용도 확장 방법 목록 내가리스트와 배열을 채우기 위해 그 코드를 삽입 할 필요가
List<Product> myProducts = new List<Product>();
myProducts.FillData();
을 채우기 위해?
목록의 내용으로 변환하여 배열로 변환 하시겠습니까? –
또는 사용자 정의 확장 메소드를 구현하고 호출하는 방법을 묻는 중입니까? – tyh
'List .ToArray()'메서드를 사용하십시오. http://msdn.microsoft.com/en-us/library/x303t819.aspx –
mao47