Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

Need more efficient LINQ

I have this SQL Code and converted to the below LINQ to Entities.  I generated the LINQ code but think there must be a better way.  I am trying to implement a Sequence column value.   The table already has an identity column and  a sequence for each item

SELECT MAX(COALESCE(FailureSequence, 0)) + 1 AS NextSequence
FROM FunctionalFailures
WHERE(FunctionalAnalysisId = 2);

Open in new window


and this LINQ

from FunctionalFailures in
(from FunctionalFailures in db.FunctionalFailures
where
  FunctionalFailures.FunctionalAnalysisId == 2
select new {
  Column1 = ((Int32?)FunctionalFailures.FailureSequence ?? (Int32?)0),
  Dummy = "x"
})
group FunctionalFailures by new { FunctionalFailures.Dummy } into g
select new {
  NextSequence = (g.Max(p => p.Column1) + 1)
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

Beautiful Dustin

Was able to adapt to my code thanks
Excellent, glad to help.