使用LINQ to sql检查记录是否已存在 [英] Check if record already exist using LINQ to sql
本文介绍了使用LINQ to sql检查记录是否已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI:
我正在使用LINQ to sql with c#
我在网页上输入一个文本框后输入一个值
如果myTable中存在此值,我需要验证
HI:
I'm using LINQ to sql with c#
I have a text box on a web page , after entering a value to that box
I need to validate whither if this value exist in myTable or not
Var xmyTable = (from ymyTable in objDataContext.myTables
Select new
{
ymyTable.myCode
ymyTable.myNmae
}
请帮助
please help
推荐答案
首先将文本框的值存储在一个字符串中,并从匹配该字符串的数据库中检索值。这里我将名称作为文本框的值。
1st of all store the textbox's value in a string and retrieve value from database matching that sting. here i am taking name as the textbox's value.
string s = textBox1.text;
var xmyTable = (from ymyTable in objDataContext.myTables where ymyTable.myName == s
Select new {ymyTable.myCode,ymyTable.myNmae}).FirstorDefault();
if(xmyTable.Count>0)
{
//your logic for record found.
}
else
{
//your logic for record not found
}
您好,您可以使用lambda expresion写这样的查询
http://stackoverflow.com/questions/6330682/select-count-in-linq-to-sql-c-sharp [ ^ ]
Hi you can write query like this using lambda expresion
http://stackoverflow.com/questions/6330682/select-count-in-linq-to-sql-c-sharp[^]
if(objDataContext.myTables.Count((a)=>a.name=="some name")==0)
{
//Record not present
}
else
{
//Record is present
}
(a)=>a.name=="some name"
适用于哪种情况
hope对你有帮助。
is for where condition
hope it is helpful to you.
var v=objDataContext.myTables.FirstOrDefault(p=>p.myCode=text box value);
if (!v=Null)
{
}
希望有所帮助
Hope it helps
这篇关于使用LINQ to sql检查记录是否已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文