【问题标题】:How to Add Collection List to a List in Asp.net MVC如何将集合列表添加到 Asp.net MVC 中的列表
【发布时间】:2014-09-15 11:28:15
【问题描述】:

我在一个方法中为员工和承包商执行两个 linq 查询并转换为列表,然后我绑定到模型类中单独声明的列表。

我每次都通过将公司 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”不应该用于将集合列表绑定到一个列表,有没有办法解决这个问题,请任何人帮助我。

【问题讨论】:

  • 不,我要添加不同的列表,但是相同的类类型,这是一个问题,应该是不同的类类型吗?

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


【解决方案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();

//第二个列表类似

【讨论】:

  • 我需要得到不同的结果,就像你上面的代码一样,两个结果都在同一个列表中
【解决方案2】:

终于明白了。

只是我在不同的类中分离了访问器,例如

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

这个修改解决了我的问题。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 是的,但是我已经编写了两个具有相同类类型的列表,例如 public List EmployeeNotEnteredTimesheetList{get;set;} public List ContractorsTimesheetList{get;set;}
    • 是的,我已经完成了像您的代码这样的单独列表,只是名称不同。
    • 那么问题出在哪里?不要使用 addRange,它只是将列表附加到另一个列表。
    • 问题结果在两个列表而不是单独的列表中合并,与问题相同的对象类型有关吗?
    • @sanjay 您应该需要更新帖子中的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2014-08-22
    • 2019-05-21
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多