Middle
IQueryable interfeys ob'ektini qaytaradigan kod misolini taqdim eting.
sobes.tech AI
AIdan javob
using System;
using System.Linq;
using System.Collections.Generic;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class ProductRepository
{
private List<Product> _products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.00m },
new Product { Id = 2, Name = "Keyboard", Price = 75.00m },
new Product { Id = 3, Name = "Mouse", Price = 25.00m }
};
// Metod, IQueryable<Product> qaytaradi, bu esa so'rovni kechiktirish imkonini beradi.
public IQueryable<Product> GetQueryableProducts()
{
return _products.AsQueryable(); // AsQueryable() IEnumerable ni IQueryable ga aylantiradi
}
}
public class Program
{
public static void Main(string[] args)
{
var repository = new ProductRepository();
// IQueryable<Product> olish
IQueryable<Product> query = repository.GetQueryableProducts();
// IQueryable ga filtr shartini qo'shish.
// So'rov bu bosqichda hali bajarilmaydi.
IQueryable<Product> filteredQuery = query.Where(p => p.Price > 100.00m);
// So'rovni bajarish, materializatsiya qilganda (masalan, ToList() chaqirilganda).
// Bu yerda so'rov ma'lumot manbaiga yuboriladi (bu holda, _products).
List<Product> expensiveProducts = filteredQuery.ToList();
Console.WriteLine("Qimmatbaho mahsulotlar (Narxi > $100):");
foreach (var product in expensiveProducts)
{
Console.WriteLine($"- {product.Name} (${product.Price})");
}
Console.ReadKey();
}
}