开发者

How to search for table in LinqToSQL? [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜