1

我有以下 2 个功能 + 1 个正在进行中:

    public static IEnumerable<LockedDate> GetAllByEmployee(int employeeID)
    {
        var v = LockedDates.GetAll();
        return from p in v where p.EmployeeID == employeeID select p;
    }

    public static IEnumerable<LockedDate> GetAllByCompany()
    {
        var v = LockedDates.GetAll();
        return from p in v where p.EmployeeID == null select p;
    }

    public static List<LockedDate> GetAllForEmployee(int employeeID)
    {
        var empList = GetAllByEmployee(employeeID);
        var compList = GetAllByCompany();
        //What do???
        return from p in empList and from q in compList where p and q are not duplicate . toList()
    }

这些使用 LINQ-SQL。

LockedDate 有一个 Date 和一个 IsYearly 布尔值。如果 IsYearly 为真,则不应考虑年份。永远不应该考虑时间。

我现在需要一个函数,它将员工拥有的所有 LockedDates 和公司的所有 LockedDates 都包含在一个列表中,没有任何重复。因此,如果 IsYearly && dd/mm 是 == 或 !IsYearly && dd/mm/yyyy 是 == 则存在重复。什么可能是一种有效的、非天真的方法?

谢谢

4

2 回答 2

1
var yearly = source.Where(p => p.IsYearly)
                   .GroupBy(p => new { Month = p.Date.Month, Day = p.Date.Day })
                   .Select(g => g.First());
var nonYearly = source.Where(p => !p.IsYearly)
                      .GroupBy(p => p.Date.Date)
                      .Select(g => g.First());

return yearly.Union(nonYearly).ToList();

source可以通过以下Union方法轻松完成:

var source = GetAllByEmployee(employeeID).Union(GetAllByCompany());
于 2013-03-14T14:23:35.773 回答
0

你不能这样做吗

    var v = LockedDates.GetAll();
    return from p in v where p.EmployeeID == null || p.EmployeeID == employeeID select p;
于 2013-03-14T14:22:28.043 回答