I am having a problem using linq to sql in a C# desktop application that connects to sql server 2008. Basically if the CustId already exists in the Trans table, I do not want to insert a new row into the table. However the code listed below does not work.
**Note the CustId is not the primary key of the table. there is an identity key that is the primary key of the table.
eDataContext rptData = new eDataContext();
var eupdate = (from a in rptData.Tran
where a.Cust_ID == CustId
select a).SingleOrDefault();
if (eupdate != null)
{
log.Info(CustId + "already exists in the Trans table");
}
else
{
Trans subCustid = null;
subCustid = new Trans();
subCustid.Package_ID = pkgid;
rptData.Trans.InsertOnSubmit(subCustid);
rptData.SubmitChanges();
}
Thus can you tell me what I can do to not allow a row to be inserted into the Trans table when the CustId already exists in the table?
Loading
VulpesPosted Sep 9, 2012, 6:14 PM
Sie StePosted Sep 9, 2012, 4:23 PM
(eupdate.count()>0) has build errors.
Can you tell me how to change the errors so the count will work like it would regularly using sql?
Sukesh MarlaPosted Sep 9, 2012, 1:09 AM
var eupdate = (from a in rptData.Tran
where a.Cust_ID == CustId
select a);
if (eupdate.count()>0)
{
log.Info(CustId + "already exists in the Trans table");
}
else
{
Trans subCustid = null;
subCustid = new Trans();
subCustid.Package_ID = pkgid;
rptData.Trans.InsertOnSubmit(subCustid);
rptData.SubmitChanges();
}
Check this is correct answer if it helped.