How to search for table in LinqToSQL? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionHow can I search in a table if a record with specific parameter doesn't exist, and then insert the record into table?
MyDataContext db = new MyDataContext();
if (db.table.Where( x => x.ID == id).ToList().Count == 0 )
{
db.table.Add(MyRow);
context.SubmitChanges();
}
if(from t in context.table where t.field.Equals(parameter) select t).Count() == 0)
{
table t = new table(){ field1 = param1, field2 = param2};
context.table.InsertOnSubmit(t);
context.SubmitChanges();
}
And remember to enclose it in a transaction for possible concurrency issues.
精彩评论