TechTalk Genome v4.2

ToObject

set .ToObject()

Return Value

a null reference if set is empty; the first (and only) element of set if it contains one element

Remarks

The ToObject() method returns the only element of set or returns a null reference if the Set<T> is empty.

The method should not be called for sets that may contain more than one element. Calling the method for these kinds of sets results in different behaviours based on the database platform and the calling context. If you need to retrieve the first element of a set, the combination of Set<T>.GetRange and Set<T>.ToObject has to be used, as in the following example.

                    
Set<Employee> employees = dd.Extent<Employee>()["Name.IsLike({0})", "A%"];
Employee firstWithA = employees.GetRange(0, 1).ToObject();
                    
As a result in this example, the variable firstWithA contains either a null reference or the first employee matching the criteria.

Quick Reference

OQL
extentof(Animal)[Name == "Nemo"].ToObject()

C#
Animal nemo =
myDB.Extent<Animal>()["Name == {0}", "Nemo"].ToObject();

SQL
SELECT TOP 1 * FROM Animal WHERE Name = 'Nemo'

See Also

Set<T>.ToObject | OqlReference.Chapter10