Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
I get multiple records from database using linq and i'm using foreach loop to get each record and added it to a list

but my problem is list retrieves only last record , how can i get multiple records to the list please help me out

What I have tried:

C#
public List<getsal> GetEmployeMotnhlySalary(decimal Perdaysalary, string sDesignation)
    {
        getsal obj = new getsal();
       
        try
        {

            var EmployeSalarylist = t.Tbl_ResourceTimeSheet.Select(x => new { x.WorkOrderNO, x.Designation, x.ResourceName, x.ResourceID, x.EmployeeID, x.AttendencePeriod, x.NumberofDaysWorked, x.NumberofDaysInMonth, x.Remarks, x.Month, x.Year }).Where(x => x.WorkOrderNO == ddlWorkOrder.Text && x.Month == DLMonth.Text && x.Year == DLYear.Text && x.Designation == sDesignation).ToList();

            
            foreach (var item in EmployeSalarylist)
            {
                
                    //getsal obj = new getsal();
                    obj.OrderNO = item.WorkOrderNO;
                    obj.Name = item.ResourceName;

                    obj.EmpID = item.EmployeeID;
                    obj.AttendedDays = item.NumberofDaysWorked;
                    obj.WorkingDays = item.NumberofDaysInMonth;
                    obj.Remarks = item.Remarks;
                    decimal DEBITAMT = Math.Round((Convert.ToDecimal(item.NumberofDaysWorked) * Perdaysalary), 0);

                    //obj.Month = item.Month;
                    //obj.Year = item.Year;
                    obj.AttendencePeriod = item.AttendencePeriod;
                    obj.Renumeration = TxtSalary.Text;
                    obj.ActualRenumeration = DEBITAMT;
                    obj.EPF = Math.Round(((DEBITAMT * Convert.ToDecimal(txtEmployershareEPF.Text)) / 100), 0);
                    if ((txtEmployershareESI.Text).Trim() != "0")
                    {
                        obj.ESI = Math.Round(((DEBITAMT * (Convert.ToDecimal(txtEmployershareESI.Text))) / 100), 0);
                    }
                    obj.AgencyCommision = Math.Round(((DEBITAMT * Convert.ToDecimal(txtAgencyCommission.Text)) / 100), 0);

                    decimal WithTax = (DEBITAMT + obj.EPF + obj.ESI + obj.AgencyCommision);
                    obj.SubTotal = WithTax;
                    obj.ServiceTax = Math.Round(((WithTax * Convert.ToDecimal(txtServiceTAX.Text)) / 100), 0);
                    obj.Sbc = Math.Round(((WithTax * Convert.ToDecimal(TxtSbc.Text)) / 100), 0);
                    obj.Total = Math.Round((WithTax + obj.ServiceTax + obj.Sbc), 0);
                    li.Add(obj);

            }

        }
        catch (Exception ex)
        {

        }

        return li;

    }
Posted
Updated 6-Apr-21 0:44am
v2

1 solution

try this, check the inline comments

C#
  public List GetEmployeMotnhlySalary(decimal Perdaysalary, string sDesignation)
{
getsal obj = new getsal();  // remove this line
 
{

var EmployeSalarylist = t.Tbl_ResourceTimeSheet.Select(x => new { x.WorkOrderNO, x.Designation, x.ResourceName, x.ResourceID, x.EmployeeID, x.AttendencePeriod, x.NumberofDaysWorked, x.NumberofDaysInMonth, x.Remarks, x.Month, x.Year }).Where(x => x.WorkOrderNO == ddlWorkOrder.Text && x.Month == DLMonth.Text && x.Year == DLYear.Text && x.Designation == sDesignation).ToList();


foreach (var item in EmployeSalarylist)
{

getsal obj = new getsal();  // uncomment this line
obj.OrderNO = item.WorkOrderNO;



Since getsal is a class which is of reference type and the instance is created once globally outside the loop, so in each iteration of the loop you are just replacing the old values with the new value, at the end of the loop, the last item will be present in all the items of the list..
 
Share this answer
 
v6
Comments
Member 123CC 20-May-16 9:17am    
thank you its working
Karthik_Mahalingam 20-May-16 9:21am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900