0

我有一个名为“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

例如,系统在 03-jan-13 时自动没有在记录下插入 draftid=20, user=user1 因为之前在 01-jan-13 插入

  Trackid       draftid     userid      date
    3       20      user1       03-jan-13 (not inserted automatically)
4

1 回答 1

0

您需要检查是否存在具有相同 TrackID、DraftID 和 UserID 的记录

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.");
于 2013-01-13T06:54:39.697 回答