C# 在icollection上查找特定项

C# 在icollection上查找特定项,c#,asp.net,linq,icollection,C#,Asp.net,Linq,Icollection,我正在开发一个web api,并试图通过icollection按名称查找一个产品,特别是一个与给定名称相匹配的产品(?name={name}) 目前我有: [HttpGet("name", Name = "GetProductByName")] public ActionResult<Product> GetByName(string _name) { var prod = (from x in _context.Products where

我正在开发一个web api,并试图通过icollection按名称查找一个产品,特别是一个与给定名称相匹配的产品(?name={name})

目前我有:

[HttpGet("name", Name = "GetProductByName")]
public ActionResult<Product> GetByName(string _name)
{
    var prod = (from x in _context.Products 
                where x.Name == _name 
                select x).FirstOrDefault();

    if (prod == null)
    {
        return NotFound();
    }
    return prod;
}
[HttpGet(“name”,name=“GetProductByName”)]
公共操作结果GetByName(字符串_名称)
{
var prod=(来自x in _context.Products
其中x.Name==\u Name
选择x).FirstOrDefault();
if(prod==null)
{
返回NotFound();
}
返回产品;
}
但是每当我查询api(api/product/?name={name}时,我都会得到所有结果

我做错了什么

编辑:控制器的剩余部分,因为它不是参数不匹配。我使用的是EF DbSet

[Route("api/Product")]
[ApiController]
public class ProductController : ControllerBase
{
    private readonly OrderingProductsContext _context;

    public ProductController(OrderingProductsContext context)
    {
        _context = context;
    }


    [HttpGet]
    public ActionResult<List<Product>> GetAll()
    {
        return _context.Products.ToList();
    }


    [HttpGet("{id}", Name = "GetProduct")]
    public ActionResult<Product> GetById(long id)
    {
        var prod = _context.Products.Find(id);
        if (prod == null)
        {
            return NotFound();
        }
        return prod;
    }


    [HttpPost]
    public IActionResult Create(Product prod)
    {
        _context.Products.Add(prod);
        _context.SaveChanges();

        return CreatedAtRoute("GetProduct", new { id = prod.ID }, prod);
    }
[路线(“api/产品”)]
[ApiController]
公共类ProductController:ControllerBase
{
私有只读OrderingProductsContext\u上下文;
公共产品控制器(OrderingProductsContext上下文)
{
_上下文=上下文;
}
[HttpGet]
公共行动结果GetAll()
{
return_context.Products.ToList();
}
[HttpGet(“{id}”,Name=“GetProduct”)]
公共操作结果GetById(长id)
{
var prod=_context.Products.Find(id);
if(prod==null)
{
返回NotFound();
}
返回产品;
}
[HttpPost]
公共IActionResult创建(产品生产)
{
_context.Products.Add(prod);
_SaveChanges();
返回CreatedAtRoute(“GetProduct”,新的{id=prod.id},prod);
}

您应该在方法定义中将
\u name
替换为
name
。根据您发布的代码,很明显
where
子句没有使用每次调用
GetByName
时传递的参数,而是使用变量
name
的值。

您得到的是
\u name
作为参数,但使用
name

改变

var prod = (from x in _context.Products where x.Name == name select x).FirstOrDefault();


首先,应该在属性参数中使用花括号

[HttpGet("{name}", Name = "GetProductByName")]
然后你可以用这个调用端点

 api/product/GetProductByName/{name}
或者,如果您想使用查询字符串调用,您可以使用

[HttpGet(Name = "GetProductByName")]
public ActionResult<Product> GetByName([FromQuery]string name)

您可以共享整个控制器吗?您可能有另一个端点来完成api/product/?name={name}查询。您的参数是“\u name”而不是“name”如我所见!这里没有
ICollection
\u context
很可能是EF DbContext。这将生成一个数据库查询。此代码甚至不应该编译我的问题并发布整个控制器,因为参数不匹配并不能解决问题。@idetodospoca我建议您在GetAll()中设置一个断点方法。我非常确定您对该方法发出了请求,但没有对GetByName发出请求。此外,您的GetByName属性缺少花括号。它应该类似于[HttpGet(“{name}”)。。。
[HttpGet(Name = "GetProductByName")]
public ActionResult<Product> GetByName([FromQuery]string name)
api/product/GetProductByName?name={name}