views:

21

answers:

0

Using SubSonic and linq-to-sql, I want to join two tables, but under a little bit different circumstances, based on runtime conditions. The tables are PriceListItem and CatalogItem. I have defined a struct, JoinedItem, that has one member for a data object from each table.

    struct JoinedItem
    {
        public PriceListItem pli;
        public CatalogItem ci;
    }

I have defined a working variable:

IEnumerable<JoinedItem> ji = null;

I assign to ji one of three IQueryables: the first creates a "WHERE x IN" SQL clause, the second joins to an in-memory list after running a SQL with no WHERE clause, and the last runs a SQL with no WHERE clause and takes all the resulting rows (You can skim this, it has little bearing on the actual problem.):

 ji =
     from pli in PriceListItem.All( )
     join ci in CatalogItem.All( ) on pli.CatalogItemID equals ci.CatalogItemID
     where guids.Contains( pli.PriceListItemID )
     select new JoinedItem { pli = pli, ci = ci };

 ji =
     from SID in priceListItemID
     join pli in PriceListItem.All( ) on SID equals pli.PriceListItemID
     join ci in CatalogItem.All( ) on pli.CatalogItemID equals ci.CatalogItemID
     select new JoinedItem { pli = pli, ci = ci };

 ji =
     from pli in PriceListItem.All( )
     join ci in CatalogItem.All( ) on pli.CatalogItemID equals ci.CatalogItemID
     select new JoinedItem{ pli = pli, ci = ci };

Whichever branch the logic takes, I eventually end up with a value for ji.

Finally, I have this line, which throws a run-time error:

foreach ( JoinedItem r in ji )

The error is an InvalidCastException, but inside I see "QueryText threw an exception of type 'System.NotSupportedException: the constant for '11111111-0000-0000-0000-123456789ABC' is not supported." (The keys for both tables are Guids.) Digging further, I see that the StackTrace blames SubSonic.Core - SubSonic.Linq.Structure.TSqlFormatter.WriteValue.

I don't have to do it this way, so I appreciate any suggestions. ALL I REALLY WANT TO ACCOMPLISH is to be able to run the exact same foreach on the results of any of three queries. In each case, I could define ji as var and run

foreach( var v in ji )

but, I not only have duplicate code, I get the same error anyway. So, I really think it is a SubSonic question more than anything.