开发者

How to search for table in LinqToSQL? [closed]

开发者 https://www.devze.com 2022-12-13 18:56 出处:网络
Closed. This question needs details or clarity.开发者_JAVA百科 It is not currently accepting answers.
Closed. This question needs details or clarity.开发者_JAVA百科 It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 8 years ago.

Improve this question

How 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消