This project has moved. For the latest updates, please go here.

Selecting objects instead of properties of objects

Topics: Developer Forum
Jun 1, 2007 at 12:05 AM
Edited Jun 1, 2007 at 12:06 AM
Is there a way to just select the objects instead of their properties?

e.g. Psuedo (this doesn't work)

SELECT a
FROM ItemA a

I expect that it can be done, since getting the object is possible via properties. Consider the following interfaces:

interface ItemA
{
ItemB B { get; }
}

interface ItemB
{
string Name { get; }
}


In this query, the ItemB's are returned:

SELECT a.B
FROM ItemA a

Whereas this query returns the properties of ItemA and not just a list of ItemA's

SELECT a.*
FROM ItemA a

Thanks
Jun 1, 2007 at 6:48 AM
In the meantime, I thought of a way to do this.

Add a property to ItemA called "Self", in which the implementation returns the object in question.

Modifying the interfaces as follows:

interface ItemA
{
ItemA Self { get; }
ItemB B { get; }
}

interface ItemB
{
ItemB Self { get; }
string Name { get; }
}

Would allow the query to return ItemA's

select a.Self
from ItemA a

Perhaps there is a better way still?
Coordinator
Jun 4, 2007 at 4:05 PM
Edited Jun 4, 2007 at 4:08 PM

chrisstefano wrote:
Is there a way to just select the objects instead of their properties?

Yes, there is.


e.g. Psuedo (this doesn't work)

  SELECT a
  FROM ItemA a
I expect that it can be done, since getting the object is possible via properties.



So do I. In fact, your query

  SELECT  a
    FROM  ItemA a
should work as expected. If it does not, what is the error message or the actual result?


interface ItemB
{
 string Name { get; }
}

...

Whereas this query returns the properties of ItemA and not just a list of ItemA's

SELECT a.*
FROM ItemA a

This behavior is intended. I think it would be hardly to understand if this query would return all a's since this is completely different from what regular SQL does. If you want the actual row to be returned you have to strip the dot and the star, e.g

    SELECT  a
      FROM  ItemA a

This does not work if the table alias refers to a derived table, e.g.

    SELECT  D
      FROM  (
                SELECT * FROM ItemA
            ) AS D

The error message is

Row reference 'D' is not valid since the table it is referring to is a derived table.

In this case you would have to use a work around like this:

    SELECT  D.TheRow
      FROM  (
                SELECT a AS TheRow FROM ItemA a
            ) AS D
Jun 4, 2007 at 4:42 PM
My bad... I guess when I was testing it there was something else causing the error and I assumed that the syntax was wrong.

SELECT a 
FROM ItemA a
Does indeed work...

Need to take off the T-SQL cap and put on the NQuery one :-)