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

asked on

Need LINQ to generate NextSequexntial number for a given Asset Id

I am trying to maintain a sequence counter in a table per another column, i will call [AssetId]

I need to LINQ to Entities in C#

Trying something like this
int? iAssetId=1

var nextSequence = db.Failures.Where(w=> w.AssetId== iAssetId).Max(x => (int?)x.NextSequence ?? 0) + 1;

Open in new window


SQL table looks like this
CREATE TABLE [dbo].[Failures] (
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [AssetId] [int] NULL,
      [NextSequence] [int] NULL
)

this must work if there is no current AssetId and it  is null
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany 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

Thanks but I need a LINQ solution

You may be right though,  I was going to first see if it has a record,  if not return 1 else do what I have.

If I don't get another response I will award you the points and close it out as undo able as you responded
ASKER CERTIFIED SOLUTION
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
I got it to work with more code.  You were helpful in letting me know why i could not get it to work.  I could have just kept trying without it