Performing common operations on two different table classes using an interface

2 views
Skip to first unread message

Matt Dargavel

unread,
Aug 27, 2009, 5:52:02 AM8/27/09
to DbLinq
Hi there,

I have a couple of tables that have a few fields that are the same
and I do some common operations on both the tables. To try and
minimise code duplication I created an interface that declared these
common fields and applied the interface to both table classes.

This let me create a Generic function that did the query I wanted to
do. As a simplified example:

private static IEnumerable<T> PrefixMatch<T>(this IQueryable<T> query,
string searchValue) where T : IRoutingGroupLookup
{
var lookups = query.Where(l => (l.LookupType ==
RoutingLookupType.Prefix)).OrderByDescending(l =>
l.LookupValue.Length);

return lookups;
}

And the code to use it:

MyDb db = new MyDb(conn);
db.InboundRoutingGroupLookups.PrefixMatch(myPrefix).FirstOrDefault();
db.OutboundRoutingGroupLookups.PrefixMatch(myPrefix).FirstOrDefault();

When trying to run the query, DBLinq throws an ArgumentException in
SqlProvider.GetLiteral(...). The operation type it's trying to do is
a MemberAccess in the where clause. If I replace the generic function
with two identical functions that use the actual table classes then it
all works ok. I tried looking through the source code but couldn't
work out where the Expression creation wasn't recognising the member
as a table column.

Is it possible to do this kind of thing? If so, if anyone could
give me some pointers on where I should look they'd be gratefully
received!

Best Regards,

Matt.

Jonathan Pryor

unread,
Sep 21, 2009, 4:47:06 PM9/21/09
to dbl...@googlegroups.com
This has been fixed in r1226.

- Jon
Reply all
Reply to author
Forward
0 new messages