Here i will explain how you can create your own class as list with linq query filtration and avoid the problem of boxing and unboxing and more reusable codes.
Make you own class like below and initialize its properties:
Now put that class in your list and insert values like this
Make you own class like below and initialize its properties:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Linq
{
class Employee
{
private int _id ;
private string _name, _address,
_department, _designation;
private DateTime _dateofbirth, _dateofjoining;
private double _salary;
private long _mobile;
public long Mobile
{
get { return _mobile; }
set { _mobile = value; }
}
public double Salary1
{
get { return _salary; }
set { _salary = value; }
}
public DateTime Dateofjoining
{
get { return _dateofjoining; }
set { _dateofjoining = value; }
}
public DateTime Dateofbirth
{
get { return _dateofbirth; }
set { _dateofbirth = value; }
}
public string Designation
{
get { return _designation; }
set { _designation = value; }
}
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Department
{
get { return _department; }
set { _department = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}
public class MysampleApp
{
List<Employee> emp;
public void myapps()
{
emp = new List<Employee> {
new Employee{Id=1,Name="shoeb",
Mobile=9820261547,Address="abc",
Dateofbirth=new DateTime(1990,07,26),
Dateofjoining=new DateTime(2011,10,26),
Department="IT",
Designation="Developer",
Salary1=100000.0},
new Employee{Id=2,Name="abdul",
Mobile=9820261547,Address="xyz",
Dateofbirth=new DateTime(1989,07,25),
Dateofjoining=new DateTime(2011,04,26),
Department="IT",
Designation="Developer",
Salary1=100000.0},
new Employee{Id=2,Name="imtiyaz",
Mobile=9820261547,
Address="pqr",
Dateofbirth=new DateTime(1985,07,26),
Dateofjoining=new DateTime(2088,07,26),
Department="Ac",
Designation="Tester",
Salary1=100000.0}
};
}
}
Now create mathod to access the list for simplicity like this
public List<Employee> GetEmployeeList()
{
if (emp == null)
myapps();
return emp;
}
Now you can query like this
List<Employee> e = GetEmployeeList();
var x = from m
in e
where m.Id == 1
select m;
foreach (var item in x)
{
Console.WriteLine("Name :" +
item.Name + " Phonenumber :" + item.Mobile);
}
following query will give you the record which have Id=1.
Thanks
please do correct me if something wrong
No comments:
Post a Comment