我们如何使用LINQ获得max substring值 [英] how we get max substring value using LINQ
本文介绍了我们如何使用LINQ获得max substring值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
受到尊重,我想使用LINQ to SQL,
通过我的应用程序中的简单编程我的代码是
private string getuniqueID()
{
con.OpenConnection();
int regcount = -1;
string strregister = ;
string str = select(MAX( SUBSTRING(Registration_Id,7,3)))来自tbl_registration;
SqlCommand cmd = new SqlCommand(str,DBConnect.connection);
SqlDataReader dr = null ;
尝试
{
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;
}
}
其他
{
strregister = STUREG001;
}
}
}
else
{
strregister = STUREG001;
}
txtregid.ReadOnly = true ;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
最后
{
con.CloseConnection();
}
返回 strregister;
但是请告诉我,我应该使用LINQ而不是
字符串str =select(MAX(SUBSTRING(Registration_Id,7,3)))从tbl_registration中写入get max substring值;
i想要使用linq这个概念,
解决方案
试试 LINQ to SQL [ ^ ]。
如下所示
var result = tbl_registration.Max(p = > int .Parse(p.Registration_Id.Substring( 7 , 3 )));
respected,i want to use LINQ to SQL,
by simple programming in my application my code is
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,
解决方案
Have a try at LINQ to SQL[^].
like below
var result = tbl_registration.Max(p => int.Parse(p.Registration_Id.Substring(7, 3)));
这篇关于我们如何使用LINQ获得max substring值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文