score:0
I think you're going to have to simply pass the DataContext to the code (manually). Sorry.
score:2
Yes - using reflection is about the only way to determine the DataContext to which the query belongs. It's the same with the Data Objects that are created when the query is triggered.
What follows doesn't strictly answer Rune's question, but may be helpful if you want to use reflection to determine whether a Data Object as attached to and monitored by a Data Context:
The following code defines a Context property which can be placed onto a data object, and then used to return the DataContext (if any) that the object is attached to.
Private Const StandardChangeTrackerName As String = "System.Data.Linq.ChangeTracker+StandardChangeTracker"
Private _context As DataClasses1DataContext
Public Property Context() As DataClasses1DataContext
Get
Dim hasContext As Boolean = False
Dim myType As Type = Me.GetType()
Dim propertyChangingField As FieldInfo = myType.GetField("PropertyChangingEvent", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim propertyChangingDelegate As PropertyChangingEventHandler = propertyChangingField.GetValue(Me)
Dim delegateType As Type = Nothing
For Each thisDelegate In propertyChangingDelegate.GetInvocationList()
delegateType = thisDelegate.Target.GetType()
If delegateType.FullName.Equals(StandardChangeTrackerName) Then
propertyChangingDelegate = thisDelegate
hasContext = True
Exit For
End If
Next
If hasContext Then
Dim targetField = propertyChangingDelegate.Target
Dim servicesField As FieldInfo = targetField.GetType().GetField("services", BindingFlags.NonPublic Or BindingFlags.Instance)
If servicesField IsNot Nothing Then
Dim servicesObject = servicesField.GetValue(targetField)
Dim contextField As FieldInfo = servicesObject.GetType.GetField("context", BindingFlags.NonPublic Or BindingFlags.Instance)
_context = contextField.GetValue(servicesObject)
End If
End If
Return _context
End Get
Set(ByVal value As DataClasses1DataContext)
_context = value
End Set
End Property
Take care to note that the object can only locate it's DataContext if it is currently attached to the context with ChangeTracking switched on. This property relies on the fact that the DataContext has subscribed to the object's OnPropertyChanging event to monitor changes over the lifespan of the object.
If this was helpful, please up-vote this post.
For more info on using reflection to find event handlers:
Source: stackoverflow.com
Related Articles
- Determine the source DataContext for a Linq to Sql query
- How do I determine the source of unpredictable LINQ query results?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Fastest way to fill DataTable from LINQ query using DataContext
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Simple LINQ query to Delete From DataContext Where ID == ID
- LINQ Source Code Available
- How can I determine if a LINQ query is going to be LINQ to SQL vs. LINQ to Objects?
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- LINQ query returns old results when source list is re-initialized
- How to get SQL query into LINQ form in C# code
- How can I code a Linq query to do an upward Include?
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Query Determine Input is in List Boundaries?
- Identify source of linq to sql query
- NHibernate LINQ query performance, which code fragment is better?
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- convert linq to object query to sql query (no linq to sql code or datacontext)
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Converting foreach loop to LINQ query breaks code
- C# code for equivalent LINQ query
- How to Select top (5) contributors group by Business type code in c# linq query
- How can I code numerous MIN functions into one LINQ to DataSet query
- Avoiding repeating code with Linq query + optional params
- Highest score data with more than one result - LINQ
- Child Objects Filter In EF
- LINQ update question comparing to SQL update. need help
- Duplicate results with GroupBy using Linq Query Expression but not using Fluent Syntax
- Linq Grouping and averages
- Check if List<T> element contains an item with a Particular Property Value
- How do I count the number of rows that have a value in column "A", but have a different value in column "B" using Linq
- Compare user's input against prefabricated text chain
- Designing an append-only data access layer with LINQ to SQL
- How to search a text contains or not in particular column that contain Json object using linq
- How to filter a collection using LINQ to DataSet
- Add ROW_NUMBER to a LINQ query for specific Entity?
- Group By and To Dictionary in EF Core 3.1
- Entity Framework 4.3 - Polymorphic Query With Eager Loading
- How to intersect in-memory data with data from database
- Leading Zeros on ip number
- How to use Linq to set attributes based on counter
- How can I sort a string of text followed by a number using LINQ
- Performance improvement for Lambda using Array.Contains?
- MVC3 - Sorting db query when using Include