Update the list data?

Posted by Mandlaa under C# on 7/14/2014 | Points: 10 | Views : 428 | Status : [Member] | Replies : 2
List<Employee> empList = new List<Employee>
{

new Employee() { EmpNo=101,EmpName="Anil",
AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"}},
DeptName="DEV",
AllSales=new List<Sales> { new Sales (){ SName="jjj",Sloc="ban",Orders=new List<Order>{new Order (){Oid=200,OName="ebay"}}}},
Purchages=new List<Purchage> {new Purchage () {Id=200,IName="Mobiles"}
//new Department (){ DeptNo=2,Dname="DEv",Location="Gun"},
//new Department (){ DeptNo=3,Dname="pro",Location="Ong"}


}}
};
}

public static class LinqUpdates
{

public static void Update<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
action(item);
}

}


i WANT TO UPDATE THE ABOVE LIST USING THE BELOW METHOS HOW?

i AM TRYING THIS WAY NOT POSSIBLE

Method is

public void Update(Employee obj)
{
var list = loadList;
loadList.Where(p => p.EmpNo == obj.EmpNo).Update();
}


I want to update whole List Ex: empList

in that list 3 classes are their

Update data the total list element by element ?

not possible to update single statement update whole list

like EX: empList.Update(obj)




Responses

Posted by: Bandi on: 7/15/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link for updating an element with a List using LINQ
http://stackoverflow.com/questions/2449449/how-to-update-an-element-with-a-list-using-linq-and-c-sharp

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 7/15/2014 [Member] Starter | Points: 25

Up
0
Down
{
[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }

[DataMember]
public string EmpName { get; set; }



//[DataMember]
//public List<Sales> AllSales { get; set; }

[DataMember]
public string DeptName { get; set; }

[DataMember]
public List<Department> AllDepart { get; set; }
[DataMember]
public List<Sales> AllSales { get; set; }

[DataMember]
public List<Purchage> Purchages { get; set; }

}
[DataContract]
public class Order
{
[DataMember]
public int Oid { get; set; }

[DataMember]
public string OName { get; set; }

//[DataMember]
//public List<Sales> Sa { get; set; }
}

[DataContract]
public class Purchage
{
[DataMember]
public int Id { get; set; }

[DataMember]
public string IName { get; set; }
}

[DataContract]
public class Department
{
[DataMember]
public int DeptNo { get; set; }

[DataMember]
public string Dname { get; set; }

[DataMember]
public string Location { get; set; }
}
[DataContract]
public class Sales
{
[DataMember]
public string SName { get; set; }

[DataMember]
public string Sloc { get; set; }

[DataMember]
public List<Order> Orders { get; set; }


}

public partial class EmployeeData
{
private static readonly EmployeeData _instance = new EmployeeData();

private EmployeeData() { }

public static EmployeeData Instance
{
get
{
return _instance;
}
}

List<Employee> empList = new List<Employee>
{

new Employee() { EmpNo=101,EmpName="Anil",
AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"}},
DeptName="DEV",
AllSales=new List<Sales> { new Sales (){ SName="jjj",Sloc="ban",Orders=new List<Order>{new Order (){Oid=200,OName="ebay"}}}},
Purchages=new List<Purchage> {new Purchage () {Id=200,IName="Mobiles"}


}}
};
public List<Employee> EmployeeList
{
get
{
return empList;
}
}

public void Add(Employee newEmployee)
{
empList.Add(new Employee
{
EmpNo = newEmployee.EmpNo,
EmpName = newEmployee.EmpName,
DeptName = newEmployee.DeptName,

});
}

public void UpdateEmp(Employee obj)
{

empList.Where(p => p.EmpNo == obj.EmpNo).Update(p => p.EmpName = obj.EmpName);

}
}

public static class LinqUpdates
{

public static void Update<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
action(item);
}

}

}

Actually this is my total code,

The using this update method ,i want to update total List of data,

The below list data i want to update
List<Employee> empList = new List<Employee>
{

new Employee() { EmpNo=101,EmpName="Anil",
AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"}},
DeptName="DEV",
AllSales=new List<Sales> { new Sales (){ SName="jjj",Sloc="ban",Orders=new List<Order>{new Order (){Oid=200,OName="ebay"}}}},
Purchages=new List<Purchage> {new Purchage () {Id=200,IName="Mobiles"}


}}
};


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response