0

Ladislav, sorry maybe i'm not correctly posed question. Suppose in WinForm application i'm creating new object:

        var newEmission = _emissionBindingSource.Current as Emission;
        if (newEmission == null) return;
        newEmission.SharesNumber = _context.GetNewSharesNumber();

    public int GetNewSharesNumber()
    {
        return Shares.Max(s => s.Number)+1;
    }`

but Max s.Number is not changing untill i havn't saved previous inserted object. So, in this case i must save every object separatly?

UPDATE 1 Suppose i have created new object emission. I took Shares.Max(s => s.Number)+1 to SharesNumber field of new object. Now i want create second emission object, and SharesNumber of this object must be greater to 1 then previous object. I can achieve this only if i save firstly created object.

3
  • If this question is only clarification of your first question you should not start a new one but instead modify the old one. Commented May 31, 2011 at 9:08
  • That needs little bit more explanation to understand what you are trying to do. Generally doing Shares.Max is bad anyway because if your application will be used by multiple users they can create emissions with the same number. Commented May 31, 2011 at 9:10
  • Ladislav, thank you for your tips. Suppose i have created new object emission. I took Shares.Max(s => s.Number)+1 to SharesNumber field of new object. Now i want create second emission object, and SharesNumber of this object must be greater to 1 then previous object. I can achieve this only if i save firstly created object. Commented May 31, 2011 at 11:09

1 Answer 1

1
    var newEmission = _emissionBindingSource.Current as Emission;
    if (newEmission == null) return;
    newEmission.SharesNumber = _context.GetNewSharesNumber();

public int GetNewSharesNumber()
{
    // this will always query DB for max value
    return Shares.CreateSourceQuery().Max(s => s.Number)+1;
}

But I believe you should use some sort of stored procedure to assign new number or use auto increment column because if you have multiple users using multiple applications, there may be the case of duplicate numbers.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, i can't find CreateSourceQuery() method in Shares objectSet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.