Assume the table below is a client table:
Client ID |
Client Name |
Client Services |
Client City |
Client Revenue |
Client type |
1 |
BK Associates |
Commercial |
Toronto |
230,000 |
active |
6 |
Bick |
Industrial |
Dallas |
679,000 |
inactive |
19 |
TPH |
Government |
Atlanta |
986,000 |
active |
12 |
Crow |
Industrial |
Phoenix |
126,000 |
inactive |
56 |
TX Electric |
Industrial |
Portland |
564,000 |
active |
42 |
GRB |
Government |
Omaha |
437,000 |
inactive |
98 |
LB&B |
Commercial |
Toronto |
990,000 |
active |
44 |
H&P |
Commercial |
Denver |
122,000 |
active |
You are asked to implement a C# console application to perform the following queries and display the results:
1. Use LINQ to sort the data by Client ID
2. Use LINQ to sort the data by Name and City
3. Use LINQ to select the Government clients and sort the results by Revenue
4. Use LINQ to select only the Active clients and calculate their total Revenue
5. Use LINQ to retrieve and display the most important Client
In case of any query, do comment. Please rate answer. Thanks
Code:
========== ClientTypeEnum.cs=======
namespace LinqData
{
public enum ClientTypeEnum
{
Inactive=0,
Active=1
}
}
================ ClientServiceEnum.cs==========
namespace LinqData
{
public enum ClientServiceEnum
{
Commerical =0,
Industrial =1,
Government=2
}
}
==================Client.cs==================
using System.Globalization;
namespace LinqData
{
public class Client
{
public int ClientId { get; set; }
public string ClientName { get; set; }
public ClientServiceEnum ClientService { get; set; }
public string ClientCity { get; set; }
public decimal ClientRevenue { get; set; }
public ClientTypeEnum ClientType { get; set; }
public override string ToString()
{
return string.Format(CultureInfo.CreateSpecificCulture("en-US"),
"Client Information : Id : {0}, Name : {1}, Service: {2}, City : {3}, Revenue : {4:c2}, Type : {5}",
ClientId, ClientName, ClientService.ToString(),ClientCity,ClientRevenue,ClientType.ToString());
}
}
}
=================Program.cs (main driver program)====================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace LinqData
{
class Program
{
static void Main(string[] args)
{
List<Client> Clients = new List<Client>();
//fill Clients data
Clients.Add(new Client()
{
ClientId = 1,
ClientName = "BK Associates",
ClientService = ClientServiceEnum.Commerical,
ClientCity = "Toronto",
ClientRevenue = 230000,
ClientType = ClientTypeEnum.Active
});
Clients.Add(new Client()
{
ClientId = 6,
ClientName = "Bick",
ClientService = ClientServiceEnum.Industrial,
ClientCity = "Dallas",
ClientRevenue = 679000,
ClientType = ClientTypeEnum.Inactive
});
Clients.Add(new Client()
{
ClientId = 19,
ClientName = "TPH",
ClientService = ClientServiceEnum.Government,
ClientCity = "Atlanta",
ClientRevenue = 986000,
ClientType = ClientTypeEnum.Active
});
Clients.Add(new Client()
{
ClientId = 12,
ClientName = "CROW",
ClientService = ClientServiceEnum.Industrial,
ClientCity = "Phoenix",
ClientRevenue = 126000,
ClientType = ClientTypeEnum.Inactive
});
Clients.Add(new Client()
{
ClientId = 56,
ClientName = "TX Electric",
ClientService = ClientServiceEnum.Industrial,
ClientCity = "Portland",
ClientRevenue = 564000,
ClientType = ClientTypeEnum.Active
});
Clients.Add(new Client()
{
ClientId = 42,
ClientName = "GRB",
ClientService = ClientServiceEnum.Government,
ClientCity = "Omaha",
ClientRevenue = 437000,
ClientType = ClientTypeEnum.Inactive
});
Clients.Add(new Client()
{
ClientId = 98,
ClientName = "LB&B",
ClientService = ClientServiceEnum.Commerical,
ClientCity = "Toronto",
ClientRevenue = 990000,
ClientType = ClientTypeEnum.Active
});
Clients.Add(new Client()
{
ClientId = 44,
ClientName = "H&P",
ClientService = ClientServiceEnum.Commerical,
ClientCity = "Denver",
ClientRevenue = 122000,
ClientType = ClientTypeEnum.Active
});
//print client information
PrintClients(Clients);
//To sort the data by Client ID
var clientOrderById= Clients.OrderBy(item => item.ClientId).ToList();
Console.WriteLine("\n\n************Print Clients after sort by Client Id****************");
PrintClients(clientOrderById);
Console.WriteLine("*****************************************************************");
//to sort the data by Name and City
var clientByNameByCity = Clients.OrderBy(item => item.ClientName).ThenBy(x=> x.ClientCity).ToList();
Console.WriteLine("\n\n********Print Clients after sort by Client Name and City*********");
PrintClients(clientByNameByCity);
Console.WriteLine("*****************************************************************");
//select the Government clients and sort the results by Revenue
var governmentClientByRevenue = Clients.Where(x=>x.ClientService == ClientServiceEnum.Government).OrderBy(y => y.ClientRevenue).ToList();
Console.WriteLine("\n\n******Print Government Clients after sort by Revenue*********");
PrintClients(governmentClientByRevenue);
Console.WriteLine("*****************************************************************");
//to select only the Active clients and calculate their total Revenue
//select the Government clients and sort the results by Revenue
var activeClients = Clients.Where(x => x.ClientType == ClientTypeEnum.Active).Sum(x=>x.ClientRevenue);
Console.WriteLine(string.Format(CultureInfo.CreateSpecificCulture("en-US"),"\n\nTotal revenue of Active Clients: {0:c2}",activeClients));
//to retrieve and display the most important Client, I am using the criteria of Highest revenue
var importantClient = Clients.OrderByDescending(x => x.ClientRevenue).FirstOrDefault();
Console.WriteLine("\n****************************Most Important Client**********************");
Console.WriteLine(importantClient);
Console.ReadLine();
}
/// <summary>
/// print client data
/// </summary>
/// <param name="clients"></param>
static void PrintClients(List<Client> clients)
{
foreach (var client in clients)
{
Console.WriteLine(client);
}
}
}
}
================screen shot of the code================
Output:
Assume the table below is a client table: Client ID Client Name Client Services Client City...