Sobes.tech
Middle

IQueryable interfeys obyektini qaytaran kod nümunəsi təqdim edin.

sobes.tech Süni İntellekt

AI-dan cavab

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> qaytarır, bu isə sorğunun yerinə yetirilməsini təxirə salmağa imkan verir.
    public IQueryable<Product> GetQueryableProducts()
    {
        return _products.AsQueryable(); // AsQueryable() IEnumerable-i IQueryable-ya çevirir
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var repository = new ProductRepository();
        
        // IQueryable<Product> əldə edin
        IQueryable<Product> query = repository.GetQueryableProducts();

        // IQueryable-ya filtr şərti əlavə edin.
        // Sorğu bu mərhələdə hələ yerinə yetirilmir.
        IQueryable<Product> filteredQuery = query.Where(p => p.Price > 100.00m);

        // Sorğunu yerinə yetirin, materializasiya zamanı (məsələn, ToList() çağırılarkən).
        // Burada sorğu məlumat mənbəsinə göndərilir (bu halda, _products).
        List<Product> expensiveProducts = filteredQuery.ToList();

        Console.WriteLine("Bahalı məhsullar (Qiymət > $100):");
        foreach (var product in expensiveProducts)
        {
            Console.WriteLine($"- {product.Name} (${product.Price})");
        }

        Console.ReadKey();
    }
}