Hi, i'm using LINQ To SQL to perform an insert via db.table.InsertOnSubmit(). I'm wondering if there is a way to reproduce the T-SQL version of the 'where not exists (select etc etc) begin insert into etc etc end' as one single query? Thanks, Martin
+2
A:
Nothing build in as far as I know, we would have to go about finding the row manually using where and than do the Insert.
There is a possibility of race coditions in such queries. Have a look at this thread for detailed solution :
http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/b1a0eb5b-d5d3-41af-829f-bbbac47b7383/
Jomit
2009-01-15 09:24:47
+3
A:
LINQ has an extension method called Contains which allows for this functionality. This can be seen in the following example:
NorthwindDataContext dc = new NorthwindDataContext();
dc.Log = Console.Out;
var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;
foreach (var c in query) Console.WriteLine( c );
Note the negation on the where clause!
This example was from the website here.
Nick
2009-01-15 09:55:02