您可以使用Entity Framework Core从数据库中检索值并进行比较。下面是一个使用LINQ的示例:
using Microsoft.EntityFrameworkCore;
using System.Linq;
public class MyController : Controller
{
private readonly ApplicationDbContext _context;
public MyController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult CompareValues(int id, string value)
{
var item = _context.Items.FirstOrDefault(i => i.Id == id);
if (item == null)
{
// Item with that ID was not found
return NotFound();
}
if (item.Value == value)
{
// The values match
return Ok();
}
else
{
// The values do not match
return BadRequest();
}
}
}
在此示例中,我们使用ApplicationDbContext
来连接到数据库。我们使用FirstOrDefault
方法来检索具有指定ID的单个项,然后通过检查其值属性来检查它是否与传递的值匹配。如果值匹配,则返回HTTP 200(“OK”)状态代码,否则返回HTTP 400(“Bad Request”)状态代码。