Join Query
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
}
public class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public int EmployeeId { get; set; }
}
public static void Main()
{
List<Employee> employees = new List<Employee>
{
new Employee { EmployeeId = 1, Name = "John" },
new Employee { EmployeeId = 2, Name = "Jane" },
new Employee { EmployeeId = 3, Name = "Doe" }
};
List<Department> departments = new List<Department>
{
new Department { DepartmentId = 1, DepartmentName = "HR", EmployeeId =
1 },
new Department { DepartmentId = 2, DepartmentName = "Finance",
EmployeeId = 2 },
new Department { DepartmentId = 3, DepartmentName = "IT", EmployeeId =
3 }
};
var query = from emp in employees
join dept in departments on emp.EmployeeId equals
dept.EmployeeId
select new
{
EmployeeName = emp.Name,
DepartmentName = dept.DepartmentName
};
foreach (var item in query)
{
Console.WriteLine($"Employee: {item.EmployeeName}, Department:
{item.DepartmentName}");
}
}
}
2. StreamWriter
namespace JoinQuery;
using System;
using System.IO;
public class StringReaderWriterExample
{
public static void Main()
{
string sampleText = "Hello\nWorld\nWelcome to .NET!";
// Using StringReader to read from the string
using (StringReader reader = new StringReader(sampleText))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine($"Read line: {line}");
}
}
// Using StringWriter to write to a string
using (StringWriter writer = new StringWriter())
{
writer.WriteLine("Hello");
writer.WriteLine("World");
writer.WriteLine("Welcome to .NET!");
string result = writer.ToString();
Console.WriteLine($"Written text:\n{result}");
}
}
}
3 Threading
using System;
using System.Threading;
namespace ThreadSyncExample
{
class Program
{
private static readonly object _lockObject = new object();
private static int _counter = 0;
static void Main(string[] args)
{
Thread thread1 = new Thread(IncrementCounter);
Thread thread2 = new Thread(IncrementCounter);
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine($"Final Counter Value: {_counter}");
}
private static void IncrementCounter()
{
for (int i = 0; i < 1000; i++)
{
lock (_lockObject)
{
_counter++;
}
}
}
}
}