有人可以帮我重构这个C#linq业务逻辑以提高效率吗?

时间:2010-03-29 22:40:00

标签: c# linq refactoring

我觉得这不是一种使用linq的非常有效的方法。我希望这里有人会对重构提出建议。我意识到这段代码不是很漂亮,因为我一直都很匆忙。

public class Workflow
{

    public void AssignForms()
    {

        using (var cntx = new ProjectBusiness.Providers.ProjectDataContext())
        {
            var emplist = (from e in cntx.vw_EmployeeTaskLists where e.OwnerEmployeeID == null select e).ToList();
            foreach (var emp in emplist)
            {
                // if employee has a form assigned: break;

                if (emp.GRADE > 15 || (emp.Pay_Plan.ToLower().Contains("al") || emp.Pay_Plan.ToLower().Contains("ex")))
                {
                    //Assign278();
                }
                else if ((emp.Series.Contains("0905") || emp.Series.Contains("0511") || emp.Series.Contains("0110") || emp.Series.Contains("1801")) || (emp.GRADE >= 12 && emp.GRADE <= 15))
                {
                    var emptask = new ProjectBusiness.Providers.EmployeeTask();
                    emptask.TimespanID = cntx.Timespans.SingleOrDefault(t => t.BeginDate.Year == DateTime.Today.Year & t.EndDate.Year == DateTime.Today.Year).TimespanID;
                    var FormID = (from f in cntx.Forms where f.FormName.Contains("450") select f.FormID).FirstOrDefault();
                    var TaskStatusID = (from s in cntx.TaskStatus where s.StatusDescription.ToLower() == "not started" select s.TaskStatusID).FirstOrDefault();
                    Assign450((int)emp.EmployeeID, FormID, TaskStatusID, emptask);
                    cntx.EmployeeTasks.InsertOnSubmit(emptask);
                }
                else
                {
                    //Assign185();
                }

            }
            cntx.SubmitChanges();
        }

    }

    private void Assign450(int EmployeeID, int FormID, int TaskStatusID, ProjectBusiness.Providers.EmployeeTask emptask)
    {
        emptask.FormID = FormID;
        emptask.OwnerEmployeeID = EmployeeID;
        emptask.AssignedToEmployeeID = EmployeeID;
        emptask.TaskStatusID = TaskStatusID;
        emptask.DueDate = DateTime.Today;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您正在使用LINQ提供程序,请尝试使用查询而不是循环。

public void AssignForms()
{

    using (var cntx = new ProjectDataContext())
    {
        var emplist = from e in cntx.vw_EmployeeTaskLists
                      where e.OwnerEmployeeID == null
                      select e;

        var timeSpanId = cntx.Timespans.SingleOrDefault(t =>
                            t.BeginDate.Year == DateTime.Today.Year &&
                            t.EndDate.Year == DateTime.Today.Year
                            ).TimespanID;

        var formID = (from f in cntx.Forms
                      where f.FormName.Contains("450")
                      select f.FormID).FirstOrDefault();

        var taskStatusID = (from s in cntx.TaskStatus
                            let status = s.StatusDescription.ToLower()
                            where status == "not started"
                            select s.TaskStatusID).FirstOrDefault();

        var assign278 = from emp in emplist
                        let payPlanInv = emp.Pay_Plan.ToUpperInvariant()
                        where emp.GRADE > 15 || (
                              payPlanInv.Contains("AL") ||
                              payPlanInv.Contains("EX")
                              )
                        select emp;

        var assign450 = from emp in emplist
                        where (emp.Series.Contains("0905") ||
                               emp.Series.Contains("0511") ||
                               emp.Series.Contains("0110") ||
                               emp.Series.Contains("1801")
                              ) || (
                               emp.GRADE >= 12 &&
                               emp.GRADE <= 15)
                        select emp;

        var assign185 = from emp in emplist
                        where !assign278.Select(e => e.EmployeeID)
                                        .Contains(emp.EmployeeID)
                              && !assign450.Select(e => e.EmployeeID)
                                           .Contains(emp.EmployeeID)
                        select emp;

        // do inserts here
        cntx.EmployeeTasks.InsertAllOnSubmit(assign450.Select(emp =>
                     new EmployeeTask()
                     {
                         TimespanID = timeSpanId,
                         FormID = formID,
                         OwnerEmployeeID = emp.EmployeeID,
                         AssignedToEmployeeID = emp.EmployeeID,
                         TaskStatusID = taskStatusID,
                         DueDate = DateTime.Today
                     }));
        cntx.SubmitChanges();
    }
}