Generate 100 records using linq c#

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // Generate a 100 records using linq c#
        List<Employee> employees = Enumerable.Range(1, 100).Select(i => new Employee
        {
            Id = i,
            Name = "Employee " + i,
            Age = new Random().Next(22, 60),
            Department = i % 2 == 0 ? "IT" : "HR",
            Salary = new Random().Next(30000, 100000)
        }).ToList();

        // Query 1: Get employees from IT department with salary > 50,000
        var highSalaryITEmployees = employees
            .Where(e => e.Department == "IT" && e.Salary > 50000)
            .OrderByDescending(e => e.Salary)
            .Take(10) // Get top 10
            .ToList();

        Console.WriteLine("Top 10 High Salary IT Employees:");
        foreach (var emp in highSalaryITEmployees)
        {
            Console.WriteLine($"{emp.Id} - {emp.Name} - {emp.Salary}");
        }

        // Query 2: Group by department and count employees
        var departmentGroups = employees
            .GroupBy(e => e.Department)
            .Select(g => new { Department = g.Key, Count = g.Count() })
            .ToList();

        Console.WriteLine("\nEmployee Count by Department:");
        foreach (var group in departmentGroups)
        {
            Console.WriteLine($"{group.Department}: {group.Count}");
        }
    }
}

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Department { get; set; }
    public int Salary { get; set; }
}

Comments

Popular posts from this blog

Finding 2nd and 3rd Highest and Lowest Salaries Using LINQ

Sonar qube for code scan