使用linq没有得到两个列表之间的区别

时间:2014-02-06 12:54:59

标签: c# sql linq linq-to-sql linq-to-entities

我有两个列表如下:

var SeparatedEmployees = (from s in DataContext.HRM_EMP_TRMN.AsEnumerable()
                                  where s.TRMN_FINL_STUS == "SA" && ((Convert.ToDateTime(s.TRMN_EFCT_DATE)).Year).ToString() == Year && ((Convert.ToDateTime(s.TRMN_EFCT_DATE)).Month).ToString() == HRMD_COMMON.ReturnMonthName(Month)
                                  join e in DataContext.VW_HRM_EMPLOYEE on s.EMP_CODE equals e.EMP_CODE
                                  where e.ACTIVE_STATUS == "A"
                                  select new HRM_EMP_FINL_STMT_MSTModel
                                  {
                                      EMP_CODE = s.EMP_CODE,
                                      EMP_NAME = e.EMP_NAME,
                                      DIVI_CODE = e.DIVI_CODE,
                                      DIVI_NAME = e.DIVI_NAME,
                                      EMP_DESIG_CODE = e.EMP_DESIG_CODE,
                                      EMP_DESIG_NAME = e.EMP_DESIG_NAME,
                                      JOINING_DATE = HRMD_COMMON.ReturnOnlyDate(Convert.ToDateTime(e.JOINING_DATE)),
                                      TRMN_TYPE = HRMD_COMMON.ReturnTerminationType(s.TRMN_TYPE),
                                      TRMN_EFCT_DATE = HRMD_COMMON.ReturnOnlyDate(Convert.ToDateTime(s.TRMN_EFCT_DATE)),
                                      LAST_SAL_PROS_MON = HRMD_COMMON.ReturnMonthName(s.LAST_SAL_PROS_MON),
                                      FINL_STMT_REM = s.TRMN_REM

                                  }).ToList();

        var ConfirmedEmployees = (from c in DataContext.HRM_EMP_FINL_STMT_MST.AsEnumerable()
                                  where ((Convert.ToDateTime(c.FINL_STMT_DATE)).Year).ToString() == Year && ((Convert.ToDateTime(c.FINL_STMT_DATE)).Month).ToString() == HRMD_COMMON.ReturnMonthName(Month)
                                  join e in DataContext.VW_HRM_EMPLOYEE on c.EMP_CODE equals e.EMP_CODE
                                  join s in DataContext.HRM_EMP_TRMN on c.EMP_CODE equals s.EMP_CODE
                                  select new HRM_EMP_FINL_STMT_MSTModel
                                  {
                                      EMP_CODE = s.EMP_CODE,
                                      EMP_NAME = e.EMP_NAME,
                                      DIVI_CODE = e.DIVI_CODE,
                                      DIVI_NAME = e.DIVI_NAME,
                                      EMP_DESIG_CODE = e.EMP_DESIG_CODE,
                                      EMP_DESIG_NAME = e.EMP_DESIG_NAME,
                                      JOINING_DATE = HRMD_COMMON.ReturnOnlyDate(Convert.ToDateTime(e.JOINING_DATE)),
                                      TRMN_TYPE = HRMD_COMMON.ReturnTerminationType(s.TRMN_TYPE),
                                      TRMN_EFCT_DATE = HRMD_COMMON.ReturnOnlyDate(Convert.ToDateTime(s.TRMN_EFCT_DATE)),
                                      LAST_SAL_PROS_MON = HRMD_COMMON.ReturnMonthName(s.LAST_SAL_PROS_MON),
                                      FINL_STMT_REM = s.TRMN_REM
                                  }).ToList();

删除第一个列表中的项目,这些项目也在第二个列表中。

 var FinalSeparatedEmployees = (from item in SeparatedEmployees
                                      where !ConfirmedEmployees.Contains(item)
                                      select item).ToList();

也试过这个:

FinalSeparatedEmployees = SeparatedEmployees.Except(ConfirmedEmployees).ToList<HRM_EMP_FINL_STMT_MSTModel>();

但没有得到准确的结果。我错过了什么?感谢。

1 个答案:

答案 0 :(得分:1)

最好使用EMP_CODE,因为您的对象无法比较。

var ids = ConfirmedEmployees.Select(x => x.EMP_CODE).ToList();
var FinalSeparatedEmployees = (from item in SeparatedEmployees
                                  where !ids.Contains(item.EMP_CODE)
                                  select item).ToList();