Charles Baldo
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
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
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ASKER
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