在ASP.NET Core中,可以使用Entity Framework Core(EF Core)作为ORM框架来进行搜索。下面是一个简单的示例:
public class Product { [Key] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public int Quantity { get; set; } }
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions
public DbSet Products { get; set; }
}
public class ProductController : Controller { private readonly AppDbContext _dbContext;
public ProductController(AppDbContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult Search(string query)
{
// 在产品名称和描述中查找查询字符串
var products = _dbContext.Products
.Where(p => p.Name.Contains(query) || p.Description.Contains(query))
.ToList();
return View(products);
}
}
在上面的示例中,我们使用了LINQ的Where方法来过滤产品名称和描述包含查询字符串的所有产品。然后,我们将筛选后的产品列表传递给视图。
最后,我们可以在视图中呈现搜索结果。
这是一个基本示例,您可以根据自己的需求进行修改。