开发者

Access DataContext behind IQueryable

Is it possible to access t开发者_高级运维he DataContext object behind an IQueryable?

If so, how?


DataContext is specific to LINQ to SQL, so presumably you're talking about LINQ to SQL queries? If so, there's no safe way to do this - you have to resort to a hack such as using reflection to retrieve the private "context" field of the underlying DataQuery object:

static DataContext GetContext (IQueryable q)
{
  if (!q.GetType().FullName.StartsWith ("System.Data.Linq.DataQuery`1")) return null;
  var field = q.GetType().GetField ("context", BindingFlags.NonPublic | BindingFlags.Instance);
  if (field == null) return null;
  return field.GetValue (q) as DataContext;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜