This is a demo that how can we store and process a set of records using Linq and our custom class. Here I have created a class for storing information. Then processed it using Linq. Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqLearning
{
class Program
{
static void Main(string[] args)
{
IANameAndID obj1 = new IANameAndID("Maah", 1, "exit");
IANameAndID obj2 = new IANameAndID("Krishna", 2, "exit");
IANameAndID obj3 = new IANameAndID("Krishna", 3, "reduced");
IANameAndID obj4 = new IANameAndID("Pawan", 4, "exit");
IANameAndID obj5 = new IANameAndID("Pawan", 5, "exit");
List<IANameAndID> col = new List<IANameAndID>();
col.Add(obj1);
col.Add(obj2);
col.Add(obj3);
col.Add(obj4);
col.Add(obj5);
Console.WriteLine(GetAgentDetailsFromCollection("Karan"," ", col));
Console.WriteLine(GetAgentDetailsFromCollection("Krishna", "exit", col));
Console.WriteLine(GetAgentDetailsFromCollection("Krishna", "ghbfjsevbjh", col));
Console.ReadLine();
}
private static string GetAgentDetailsFromCollection(string strIAName, string strDataResponsibility, List<IANameAndID> objCol)
{
string objAgentLookup = null;
IEnumerable<IANameAndID> selectedCollection = new List<IANameAndID>();
if (objCol != null && objCol.Count > 0)
{
if (strIAName.Equals("Karan"))
{
selectedCollection = from item in objCol
where item.IAName.Equals("Maah")
select item;
if (selectedCollection.Count() == 1)
{
objAgentLookup = selectedCollection.FirstOrDefault().ID + selectedCollection.FirstOrDefault().IAName;
}
}
else
{
selectedCollection = from item in objCol
where item.IAName.Equals(strIAName)
select item;
int Count = selectedCollection.Count();
if (selectedCollection.Count() == 1)
{
objAgentLookup = selectedCollection.FirstOrDefault().ID + selectedCollection.FirstOrDefault().IAName;
}
else if (selectedCollection.Count() == 2)
{
var subselectedCollection = from item in selectedCollection
where item.TypeofProject.ToLower().Contains("exit") && strDataResponsibility.ToLower().Contains("exit")
select item;
if (subselectedCollection.Count() == 1)
{
objAgentLookup = subselectedCollection.FirstOrDefault().ID + subselectedCollection.FirstOrDefault().IAName;
}
else
{
objAgentLookup = selectedCollection.LastOrDefault().ID + selectedCollection.LastOrDefault().IAName;
}
}
else
{
var innerselectedCollection = from item in objCol
where item.IAName.Equals("No Agent Assinged")
select item;
if (innerselectedCollection.Count() == 1)
{
objAgentLookup = innerselectedCollection.FirstOrDefault().ID + innerselectedCollection.FirstOrDefault().IAName;
}
}
}
}
return objAgentLookup;
}
public class IANameAndID
{
public string IAName { get; set; }
public int ID { get; set; }
public string TypeofProject { get; set; }
public IANameAndID(string strName, int intID, string strTypeofProject)
{
IAName = strName;
ID = intID;
TypeofProject = strTypeofProject;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqLearning
{
class Program
{
static void Main(string[] args)
{
IANameAndID obj1 = new IANameAndID("Maah", 1, "exit");
IANameAndID obj2 = new IANameAndID("Krishna", 2, "exit");
IANameAndID obj3 = new IANameAndID("Krishna", 3, "reduced");
IANameAndID obj4 = new IANameAndID("Pawan", 4, "exit");
IANameAndID obj5 = new IANameAndID("Pawan", 5, "exit");
List<IANameAndID> col = new List<IANameAndID>();
col.Add(obj1);
col.Add(obj2);
col.Add(obj3);
col.Add(obj4);
col.Add(obj5);
Console.WriteLine(GetAgentDetailsFromCollection("Karan"," ", col));
Console.WriteLine(GetAgentDetailsFromCollection("Krishna", "exit", col));
Console.WriteLine(GetAgentDetailsFromCollection("Krishna", "ghbfjsevbjh", col));
Console.ReadLine();
}
private static string GetAgentDetailsFromCollection(string strIAName, string strDataResponsibility, List<IANameAndID> objCol)
{
string objAgentLookup = null;
IEnumerable<IANameAndID> selectedCollection = new List<IANameAndID>();
if (objCol != null && objCol.Count > 0)
{
if (strIAName.Equals("Karan"))
{
selectedCollection = from item in objCol
where item.IAName.Equals("Maah")
select item;
if (selectedCollection.Count() == 1)
{
objAgentLookup = selectedCollection.FirstOrDefault().ID + selectedCollection.FirstOrDefault().IAName;
}
}
else
{
selectedCollection = from item in objCol
where item.IAName.Equals(strIAName)
select item;
int Count = selectedCollection.Count();
if (selectedCollection.Count() == 1)
{
objAgentLookup = selectedCollection.FirstOrDefault().ID + selectedCollection.FirstOrDefault().IAName;
}
else if (selectedCollection.Count() == 2)
{
var subselectedCollection = from item in selectedCollection
where item.TypeofProject.ToLower().Contains("exit") && strDataResponsibility.ToLower().Contains("exit")
select item;
if (subselectedCollection.Count() == 1)
{
objAgentLookup = subselectedCollection.FirstOrDefault().ID + subselectedCollection.FirstOrDefault().IAName;
}
else
{
objAgentLookup = selectedCollection.LastOrDefault().ID + selectedCollection.LastOrDefault().IAName;
}
}
else
{
var innerselectedCollection = from item in objCol
where item.IAName.Equals("No Agent Assinged")
select item;
if (innerselectedCollection.Count() == 1)
{
objAgentLookup = innerselectedCollection.FirstOrDefault().ID + innerselectedCollection.FirstOrDefault().IAName;
}
}
}
}
return objAgentLookup;
}
public class IANameAndID
{
public string IAName { get; set; }
public int ID { get; set; }
public string TypeofProject { get; set; }
public IANameAndID(string strName, int intID, string strTypeofProject)
{
IAName = strName;
ID = intID;
TypeofProject = strTypeofProject;
}
}
}
}