Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
respected,i want to use LINQ to SQL,

by simple programming in my application my code is

C#
private string getuniqueID()
        {
                con.OpenConnection();
                int regcount = -1;
                string strregister = "";
                string str = "select (MAX(SUBSTRING(Registration_Id,7,3))) from tbl_registration";
                SqlCommand cmd = new SqlCommand(str, DBConnect.connection);
                SqlDataReader dr = null;
                try
                {
                    dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            if (!(dr.GetValue(0).ToString() == ""))
                            {
                                regcount = Convert.ToInt32(dr.GetString(0));
                                int regcountinc = regcount + 1;
                                if (regcountinc <= 9)
                                {
                                    strregister = "STUREG00" + regcountinc;
                                }
                                else
                                {
                                    strregister = "STUREG0" + regcountinc;
                                }
                            }
                            else
                            {
                                strregister = "STUREG001";
                            }
                        }
                    }
                    else
                    {
                        strregister = "STUREG001";
                    }
                    txtregid.ReadOnly = true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.CloseConnection();
                }
                return strregister;


but please tell me what should i write for get max substring value using LINQ instead of
string str = "select (MAX(SUBSTRING(Registration_Id,7,3))) from tbl_registration";
i want to use linq for this concept,
Posted
Updated 30-Apr-14 21:44pm
v3

Have a try at LINQ to SQL[^].
 
Share this answer
 
like below

C#
var result = tbl_registration.Max(p => int.Parse(p.Registration_Id.Substring(7, 3)));
 
Share this answer
 
Comments
Member 10192073 1-May-14 9:10am    
Not working

I wrote my code

Var result =from a in obj.tbl_registration.max(p=>int.parse (p.registration_id.substring (7,3))).tostring() select a

But error occur that could not translate expression

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900