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

VulpesRank #55 · 29.2K reputationPosted Sep 9, 2012, 2:14 PM EDT
Sukesh MarlaRank #461 · 5.4K reputationPosted Sep 8, 2012, 9:09 PM EDT
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.
Sie Ste196 reputationPosted Sep 9, 2012, 12:23 PM EDT
(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?