views:

255

answers:

1

Hi there,

Processing an XML file with LINQ to add records into a table in a SQL Server database via a data context. As we are adding records we need to assign the parents in various other tables. Most of the time we can find the existing parent and use it but sometimes we will need to create a new parent.

Thought we could do this like this:

Dim defaultPub As publication
defaultPub = New publication With {.name = e..Value}
Dim pub = _Data.publications.Where(Function(s) s.name = e..Value).DefaultIfEmpty(defaultPub).SingleOrDefault
.publication = pub

So we are trying to find a publication that matches e..Value from our XML, but if we can't find one then we use 'defaultPub'. If it does exist though we don't want to add it again. So maybe this approach is flawed anyway even if it did work...

Anyway it's not currently working, we get this error: Unsupported overload used for query operator 'DefaultIfEmpty'.

The overload requires a publication and is getting one (I've checked the TypeNames in quickwatch), don't know what is going on here.

What we are really looking for is something like the find_or_create from activerecord for Ruby that LINQ seems to be a copied from.

Thanks in advance,

Dave.

A: 

OK, I can just do it like this:

Dim pub = _Data.publications.Where(Function(s) s.name = e.<source>.Value).SingleOrDefault
Dim newPub As publication
If Not IsNothing(pub) AndAlso pub.name <> "" Then
   .publication = pub
Else
   newPub = New publication With {.name = e.<source>.Value}
   .publication = newPub
End If

Thanks Val!

DaveEHS