I have a WCF Data Services with an entity named Contract.
there is a many to many between Contracts and Quotes : ( * ) Contract <----> ( * ) Quotes
I have a method that adds/remove links between contracts and quotes.
and sometimes I get an updateException because I'm trying to add a link between a Contract and a Quote when the link is already existent.
I want to write a query that adds a link only if the link doesn't exist yet without needing to query the database for the existing links between my contract and quotes.
Is there a way of doing that using Expression Trees ? or Linq ?
At the moment, I do this :
void ModifyContract(Contract ctr)
{
var contractInDb = (from c in service.Contracts.Expand("Quotes")
where c.Id == ctr.Id).Single();
foreach(q in ctr.Quotes)
{
if( ! contractInDb.Quotes.Contains(q))
{
service.AddLink(ctr,"Quotes", q);
}
}
service.SaveChanges();
}