我使用linq从数据库中获取多条记录,并且我使用foreach循环来获取每条记录并将其添加到列表中 [英] I get multiple records from database using linq and I'm using foreach loop to get each record and added it to a list

查看:70
本文介绍了我使用linq从数据库中获取多条记录,并且我使用foreach循环来获取每条记录并将其添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用linq从数据库中获取多条记录,并且我使用foreach循环来获取每条记录并将其添加到列表中



但我的问题是列表检索只有最后一条记录,我如何获得多个记录到列表请帮帮我



我尝试过:



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:

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;

    }

推荐答案

试试这个,查看内联评论



try this, check the inline comments

  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;







因为 getsal 是一个引用类型实例在循环外全局创建一次,所以在循环的每次迭代中你都是只需用新值替换旧值,在循环结束时,最后一项将出现在列表的所有项目中..




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..


这篇关于我使用linq从数据库中获取多条记录,并且我使用foreach循环来获取每条记录并将其添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆