using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqDemo
{
class Program
{
/// <summary>
/// This method display string array records either in ascending order
/// or descending order on basis of order parameter. For performing
/// this task I have use LINQ. We use orderby clause to specify ordering
/// followed by variable name then order which is either ascending or
/// descending. If you do not specify order then linq generate list
/// in ascending order.
/// </summary>
public void displayValueInOrder(string[] records, string order)
{
if (order.Equals("ASC"))
{
var record = from std in records
orderby std ascending
select std;
//Displaying record in ascending order.
foreach (string data in record)
Console.WriteLine(data);
}
else
{
var record = from std in records
orderby std descending
select std;
//Displaying record in descending order.
foreach (string data in record)
Console.WriteLine(data);
}
}
/// <summary>
/// This method search record from array and display first search value.
/// searchSingleRecord() method takes two parameter first records array
/// in which search operation needs to be perform and second one is
/// search value which needs to be searched. After searching record
/// it display first value if exists from search record.
/// </summary>
public void searchSingleRecord(string[] records,string searchValue)
{
string record = (from std in records
where std.Equals(searchValue)
select std).FirstOrDefault();
Console.WriteLine(record);
}
static void Main(string[] args)
{
string[] studentName = { "Awadhendra", "Ankit", "James", "John", "Cat", "Shilpa", "Shivam", "Haider", "Ashish" };
Program pr = new Program();
pr.displayValueInOrder(studentName, "ASC");
Console.WriteLine();
pr.searchSingleRecord(studentName, "Avanish");
Console.ReadKey();
}
}
}
Thanks for reading this blog. Please provide any suggestion and feedback on this blog and suggest me some other topic in which I need to write articles or blogs.