如何在Asp.net MVC中将集合列表添加到列表中
本文关键字:列表 集合 添加 MVC Asp net | 更新日期: 2023-09-27 18:21:51
我在一个方法中为员工和承包商执行两个linq查询并转换为List,然后我绑定到模型类中单独声明的列表。
我每次都会为公司列表中的每个公司执行这个方法,方法是将公司id和模型类作为参数传递,如
public void GetEmployeeContractorsTimesheetNotEntered(int COMP_ID, string COMPANY_NAME, TimesheetModel model)
{
context = new ResLandEntities();
DateTime todayDate = DateTime.Now.Date;
DateTime thisWeekStartDate = todayDate.AddDays(-(int)todayDate.DayOfWeek).Date; //Start Date of Current Week
DateTime thisWeekEndDate = thisWeekStartDate.AddDays(6); // End Date of Current Week
var todaysDay = (int)DateTime.Now.DayOfWeek;
var employeesNotEnteredTimesheetList = (from emps in context.EMPLOYEE
join comp in context.COMPANY on emps.COMP_ID equals comp.ID
join notify in context.NOTIFICATION on emps.NOTIFICATION_ID equals notify.ID
from week in context.WEEK_CALENDER
from statlk in context.STATUS_LKUP
where !context.TIMESHEET.Any(m => m.WEEK_CAL_ID == week.ID
&& m.RES_TYPE == "EMPLOYEE"
&& m.RES_ID == emps.ID
&& m.COMP_ID == COMP_ID
&& m.IS_DELETED=="N") &&
week.WEEK_START_DT.Month == DateTime.Now.Month &&
week.WEEK_START_DT.Year == DateTime.Now.Year &&
week.WEEK_END_DT<=thisWeekEndDate &&
statlk.TYPE == "TIMESHEET" &&
statlk.STATE == "NOT_ENTERED" &&
emps.IS_DELETED == "N" &&
emps.COMP_ID==COMP_ID
select new TimesheetModel
{
EMP_ID = emps.ID,
EMP_COMP_ID = emps.COMP_EMP_ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
COMPANY_NAME = comp.NAME,
PrimaryEmail = notify.PRI_EMAIL_ID,
SDate = week.WEEK_START_DT,
EDate = week.WEEK_END_DT,
EMP_STATUS = "NOT_ENTERED"
}).Distinct().ToList();
//Adding a Collection of List Here
model.GetTimesheetNotEnteredDetails.AddRange(employeesNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList());
var contractorsNotEnteredTimesheetList = (from contrs in context.CONTRACTOR
join client in context.CLIENT on contrs.CLIENT_ID equals client.ID
join notification in context.NOTIFICATION on contrs.NOTIFICATION_ID equals notification.ID
from week in context.WEEK_CALENDER
from statlk in context.STATUS_LKUP
where !context.TIMESHEET.Any(m => m.RES_ID == contrs.ID
&& m.WEEK_CAL_ID == week.ID
&& m.COMP_ID == COMP_ID
&& m.RES_TYPE == "CONTRACTOR"
&& m.IS_DELETED == "N")
&& week.WEEK_START_DT.Month == DateTime.Now.Month
&& week.WEEK_START_DT.Year == DateTime.Now.Year
&& statlk.STATE == "NOT_ENTERED"
&& statlk.TYPE == "TIMESHEET"
&& contrs.IS_DELETED == "N"
&& week.WEEK_START_DT <= thisWeekEndDate
&& contrs.COMP_ID == COMP_ID
select new TimesheetModel
{
EMP_ID=contrs.ID,
EMPLOYEE_NAME = contrs.FIRST_NAME + " " + contrs.LAST_NAME,
COMPANY_NAME = COMPANY_NAME,
SDate=week.WEEK_START_DT,
EDate=week.WEEK_END_DT,
CLIENT_NAME = client.NAME,
PrimaryEmail = notification.PRI_EMAIL_ID
}).Distinct().ToList();
//Adding Collection of List Here
model.GetContractorNotEnteredDetails .AddRange(contractorsNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList());
}
现在,我的问题是,我想将列表集合单独添加到两个列表中,尽管我将列表单独绑定,但员工和承包商列表的两个结果将合并在两个列表上,就像员工和承包商在绑定两个列表时一样,它应该单独绑定。出了什么问题,是不是"AddRange"不应该用于将集合列表绑定到一个列表,有什么方法可以解决这个问题,请帮助我。
使用此
var props = typeof(TimesheetModel).GetProperties();
DataTable dt= new DataTable();
dt.Columns.AddRange(
props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()
);
employeesNotEnteredTimesheetList.ForEach(
i => dt.Rows.Add(props.Select(p => p.GetValue(i, null)).ToArray())
);
var list1 = (from p in dt.AsEnumerable()
select p).ToList();
//类似于第二个列表
终于拿到了。
只是我在不同的类中分离了访问者,比如
public class EmployeeTimesheetDetails
{
public int EMP_ID { get; set; }
public string EMP_COMP_ID { get; set; }
public string EMPLOYEE_NAME { get; set; }
public string COMPANY_NAME { get; set; }
public string PrimaryEmail { get; set; }
public DateTime SDate { get; set; }
public DateTime EDate { get; set; }
public string EMP_STATUS { get; set; }
}
public class ContractorsTimesheetDetails
{
public int CONTR_ID { get; set; }
public string CONTRACTOR_NAME { get; set; }
public string COMPANY_NAME { get; set; }
public DateTime SDate { get; set; }
public DateTime EDate { get; set; }
public string CLIENT_NAME { get; set; }
public string PrimaryEmail { get; set; }
}
并像一样修改了模型类中的两个列表
public List<EmployeeTimesheetDetails> GetTimesheetNotEnteredDetails { get; set;}
public List<ContractorsTimesheetDetails> GetContractorNotEnteredDetails { get; set; }
这个修改解决了我的问题。
类TimesheetModel
中需要有两个属性,如下所示:
public class CompanyListModel
{
public List<CompanyModel> CompanyList { get; set; };
}
public class CompanyModel
{
public List<TimesheetModel > EmployeesNotEnteredTimesheetList { get; set; };
public List<TimesheetModel > ContractorsNotEnteredTimesheetList { get; set; };
}
然后这样添加:
public void GetEmployeeContractorsTimesheetNotEntered(int COMP_ID, string COMPANY_NAME, CompanyListModel model)
{
// your stuff
CompanyModel conpanyModel = new CompanyModel();
conpanyModel.EmployeesNotEnteredTimesheetList = employeesNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList();
conpanyModel.ContractorsNotEnteredTimesheetList = contractorsNotEnteredTimesheetList.GroupBy(m => m.EMP_ID).Select(m => m.First()).ToList();
model.CompanyList.add(companyModel);
// your stuff
}