none
Help with difficult Linq query RRS feed

  • Question

  • I am working on a project that is basically a workflow. There is initial form submittal that creates a record with a status of 1 and notifies an approver by email that a submission awaits his review. The person reviewing form then approves or rejects. if the form is approved it's status is set to status + 1, if rejected it's status is set to zero where it can be modified and resubmitted at a status of 1.

    There are two tables one contains the form record and the other "Approvals" contains the workflow history.

    I need to prevent a user from submitting a approval or reject twice. In trying to build a linq query to see if the reviewer already approved or rejected in the current round.

    Can Someone Assist?

    Here is my query but it's not working correctly:

     

     var z = (from p in db.Approvals
                         group p by p.return_id into grp
                         let maxReturnCreated_dt = grp.Max(g => g.created_dt)
                         from p in grp
                         where p.created_dt == maxReturnCreated_dt
                         where p.return_id == rtnId
                         where p.Person_id == ReviewerID
                         select p).SingleOrDefault();

     

     

     

     

    Friday, October 28, 2011 1:30 PM

Answers

  • Hi

    Instead of using .SingleOrDefault(), you can go for .Count() which gives you the number of Approval or Rejection. In this way, you can check your scenario.

    As .SingleOrDefault() will throw exception if two sequence matches your criteria.

    If you use .Count(), you can check if the .Count() contains the number more than one sequence.


    Regards TurbulentGeek "
    Mark as answer if it helped you..."
    ;
    Friday, October 28, 2011 2:01 PM
  • var z = (from p in db.Approvals
                group p by p.return_id into grp
                let maxReturnCreated_dt = grp.Max(g => g.created_dt)
                from p in grp
                where p.created_dt == maxReturnCreated_dt && p.return_id == rtnId && p.Person_id == ReviewerID
                select p).SingleOrDefault();

    Hope this helps..

    With Best Regards, Boovendan M
    Monday, October 31, 2011 5:25 AM

All replies

  • Hi

    Instead of using .SingleOrDefault(), you can go for .Count() which gives you the number of Approval or Rejection. In this way, you can check your scenario.

    As .SingleOrDefault() will throw exception if two sequence matches your criteria.

    If you use .Count(), you can check if the .Count() contains the number more than one sequence.


    Regards TurbulentGeek "
    Mark as answer if it helped you..."
    ;
    Friday, October 28, 2011 2:01 PM
  • Hi WTFHolmes;

    If the problem is not that the query is throwing an exception as TurbulentGeek stated in his post please tell us what the error is or what is causing the difficulty.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 28, 2011 2:57 PM
  • var z = (from p in db.Approvals
                group p by p.return_id into grp
                let maxReturnCreated_dt = grp.Max(g => g.created_dt)
                from p in grp
                where p.created_dt == maxReturnCreated_dt && p.return_id == rtnId && p.Person_id == ReviewerID
                select p).SingleOrDefault();

    Hope this helps..

    With Best Regards, Boovendan M
    Monday, October 31, 2011 5:25 AM