I developed the extension method:
public static IQueryable<T> Get<T>(this DataContext dataContext) where T : class
{
return dataContext.GetTable<T>();
}
Then I tried to use it:
dataContext.Get<XDbCurrency>().
Where(c => c.Id == XCurrencyId.USD).
SelectMany(
c =>
dataContext.Get<XDbCurrencyRate>().
Where(cr =>
cr.CurrencyId == c.Id &&
cr.StartOn <= DateTime.UtcNow &&
cr.EndOn > DateTime.UtcNow).
Take(1),
(c, cr) => cr).
ToArray();
I get the exception Member access 'XCurrencyId CurrencyId' of 'X.XDbCurrencyRate' not legal on type 'System.Data.Linq.Table``1[X.XDbCurrencyRate].
When I change Get
back to GetTable
- everything works! When I change the return type of Get
from IQueryable<T>
to Table<T>
- it crashes.
UPDATE. I created XDataContextBase
class, moved Get
method there, inherited DBML-generated XDataContext
from XDataContextBase
- unfortunately it does not help. Magic!!
UPDATE. this.GetTable<XDbCurrencyRate>
works, this.XDbCurrencyRates
works, this.Get<XDbCurrencyRate>
not works either Get
is extension method or method in base class. By the way, internally this.XDbCurrencyRates
is implemented absolutely like my Get
. Magic!!
UPDATE. Looks like Linq-to-SQL supports only two things: either direct call of DataContext.GetTable<>
or call of any-named property that returns System.Data.Linq.Table<XDbCurrencyRate>
. If I create System.Data.Linq.Table<XDbCurrencyRate> Foo
- it works, but when I create IQueryable<XDbCurrencyRate> Foo
- it crashes with Queries with local collections are not supported.
. Magic!!
Any ideas?
This is because the initial Get
call is not part of the query tree. Just a normal method call. The second call is embedded in the tree and LINQ to SQL has no idea what that method means.
I know that EF would give up here immediately. I believe L2S is basically capable of running such a method locally and trying to inline the query that it returns. Here, that seems to now work out (or I'm wrong about that).
What I did was to create a custom LINQ provider that rewrites expression trees to execute such methods locally, inline their results into the query tree and then forward to LINQ to SQL. This is not trivial.
Maybe you can make AsExpandable
from the so called "LINQ kit" (approximate spelling) work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.