none
ident_current RRS feed

  • Question

  • hi friends
    i wanna retrieve last identity in SQL table with linq,in SQL i use ident_current..how can i have this in LINQ ?
    Monday, November 16, 2009 8:37 AM

Answers

  • Alternative soln for this , create a SP which accepts a parameter represents table name and returns identity value for the table , something like thid


    ALTER PROCEDURE

     

    dbo.GETIDENTITY

     

    (

    @tableName

    varchar(50)

    )

    AS

     

    /* SET NOCOUNT ON */

     

    SELECT IDENT_CURRENT (@tableName) AS Current_Identity;



    Associate this sp in your DATACONTEXT class

    and the value can be retrieved  by the below code


     

    var iden = (from a in db.GETIDENTITY("Customers")

     

    select a.Current_Identity.Value).FirstOrDefault();


    Hope this is useful in your scenario

    Wednesday, November 18, 2009 7:56 AM

All replies




  • Hi dr_csharp_first,

     

    You can try like this,

                    var i = from p in context.Stus

                            orderby p.id

                            descending select p;

                    MessageBox.Show(i.First().id.ToString());

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 18, 2009 6:39 AM
  • Alternative soln for this , create a SP which accepts a parameter represents table name and returns identity value for the table , something like thid


    ALTER PROCEDURE

     

    dbo.GETIDENTITY

     

    (

    @tableName

    varchar(50)

    )

    AS

     

    /* SET NOCOUNT ON */

     

    SELECT IDENT_CURRENT (@tableName) AS Current_Identity;



    Associate this sp in your DATACONTEXT class

    and the value can be retrieved  by the below code


     

    var iden = (from a in db.GETIDENTITY("Customers")

     

    select a.Current_Identity.Value).FirstOrDefault();


    Hope this is useful in your scenario

    Wednesday, November 18, 2009 7:56 AM
  • ALTER PROCEDURE dbo.GETIDENTITY (@tableName varchar(50) )

     

    AS

    /* SET NOCOUNT ON */

     

    var iden = (from a in db.GETIDENTITY("Customers")  

    select a.Current_Identity.Value).FirstOrDefault();


    SELECT IDENT_CURRENT (@tableName) AS Current_Identity;

     

    thanks,i think this is the best way !

    and about Yichun_Feng answer :
    i think in that way,performance is too low !
    Wednesday, November 18, 2009 8:04 AM