如何将集合列表添加到Asp.net MVC中的列表

时间:2014-09-15 11:28:15

标签: c# asp.net-mvc linq list

我正在为方法中的员工和承包商执行两个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”不应该用于绑定集合列表到一个列表,有没有办法解决这个问题,请帮助我。

3 个答案:

答案 0 :(得分:1)

使用此

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();

//类似于第二个列表

答案 1 :(得分:1)

终于明白了。

我在不同的类中分隔了Accessor,比如

 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; }

我的问题清除了此修改。

答案 2 :(得分:0)

您需要在班级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
}