I’m trying to define a new type and have not had much luck finding any information about using lists within them. Basically my new type will contain two lists, lets say x and y of type SqlSingle (the user defined type is written in C#) is this even possible?
If not how are you supposed to go about simulating a two lists of an arbitary length in an SQL Server 2008 column?
I’m possibly going about this the wrong way but it is the best approach I can think of at the moment. Any help is very much appreciated.
You can use a
List<T>in a CLR UDT – although CLR types are structs, which should be immutable, so aReadOnlyCollection<T>would be a better choice if you don’t have a very compelling reason for the mutability. What you need to know in either case is that SQL won’t know how to use the list itself; you can’t simply expose the list type as a publicIList<T>orIEnumerable<T>and be on your merry way, like you would be able to do in pure .NET.Typically the way to get around this would be to expose a
Countproperty and some methods to get at the individual list items.Also, in this case, instead of maintaining two separate lists of
SqlSingleinstances, I would create an additional type to represent a single point, so you can manage it independently and pass it around in SQL if you need to:The main type would look something like this:
If you need SQL to be able to get a sequence of all the points, then you can add an enumerable method to the sequence type as well:
You might think that it’s possible to use an
IEnumerable<T>and/or use an instance method instead of a static one, but don’t even bother trying, it doesn’t work.So the way you can use the resulting type in SQL Server is:
Hope this helps get you on the right track. UDTs can get pretty complicated to manage when they’re dealing with list/sequence data.
Also note that you’ll obviously need to add serialization methods, builder methods, aggregate methods, and so on. It can be quite an ordeal; make sure that this is actually the direction you want to go in, because once you start adding UDT columns it can be very difficult to make changes if you realize that you made the wrong choice.