我有一个名为“tbltrackuser”的表格,下面是“
Trackid (pk,identity)
Drafiid (bigint)
Userid (bigint)
Date (time)
我喜欢在第一次使用此表中的linq为特定用户和draftid插入记录,并且自动不在表中插入2次或更多次。
Trackid draftid userid date
1 20 user1 01-jan-13
2 30 user1 02-jan-13
4 20 user2 01-jan-13
例如系统自动未插入draftid = 20的记录下方,user = user1 at 03-jan-13 因为已经在01-jan-13之前插入了
Trackid draftid userid date
3 20 user1 03-jan-13 (not inserted automatically)
答案 0 :(得分:0)
您需要检查是否存在具有相同TrackID,草稿ID和用户ID
的记录tbltrackuser trackuser = new tbltrackuser();
trackuser.TrackID = 1;
trackuser.DraftID = 20;
trackuser.UserID = "user1";
trackuser.Date = "01-jan-13";
DataClasses1DataContext dc = new DataClasses1DataContext();
var sameRecords = from tu in dc.tbltrackusers
where tu.DraftID == trackuser.DraftID
&& tu.UserID == trackuser.UserID
&& tu.Date == trackuser.Date
select tu;
if (sameRecords.Count() == 0) //if there is no same record then you can insert
{
dc.tbltrackusers.InsertOnSubmit(trackuser);
dc.SubmitChanges();
}
else
MessageBox.Show("Such record already exists.");