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 Query
- Determine the source DataContext for a Linq to Sql query
- What the linq query for SQL like and Soudex for sql server 2008?
- How do I determine the source of unpredictable LINQ query results?
- Reduce the line of code for this LINQ query
- Can I Use The Same Linq Code to Query for Different Conditions?
- What SQL query or LINQ code would get the following data?
- Shortcut LINQ toentity query for the following code in MVC 3
- Relevant linq query for the SQL
- Linq to sql query for check the usernames exists in table?
- What is the easiest way to find the LINQ statement for a SQL statement
- Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator
- Steps for a beginner to run very basic linq to sql query using Linqpad
- How to force LINQ to SQL to evaluate the whole query in the database?
- Performing part of a IQueryable query and deferring the rest to Linq for Objects
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq query for only the first N rows for each unique ID
- What is the easiest way to save a LINQ query for later use?
- How can I determine if a LINQ query is going to be LINQ to SQL vs. LINQ to Objects?
- Is it possible to see the SQL query of a LINQ command?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Linq to SQL DataContext fails to update Enum field in database for inherited objects
- Create Linq Expression for Sql Equivalent "column is null" in c# by creating linq query dynamically
- 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?
- Unable to convert SQL Query to LINQ Query for Left Outer Join
- what is the mechanism for performing an intersect in a Mongo Linq query
- Create linq query to search for contact the same way smartphone will do so
More Query from same tag
- Find a range inside a range using LINQ
- Get column name by value of field in datarow
- Get minimum and maximum time value from list of object property using Linq
- linq Group By and take first and lats records
- Trouble with OR in Sharepoint CAML
- Iterate through linq query results by day to check conditions
- How to map data to correspond to table headers by looping through 3 tables from db [C# MVC & LINQ]
- Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type
- Linq - How to construct this query without the 'join' keyword?
- Can't get DropDownList value shown in FormView ItemTemplate to appear in EditItemTemplate by default
- Unit test setup never invoked
- Joining two tables in EF Core, invalid column name
- Inefficient MVC ViewModel Making Multiple Calls to the Database?
- Grouping in Linq queries with multiple tables
- Combining data using Linq
- Use LINQ to aggregate on a list of lists
- Conditioning LINQ Query
- C# Create xml from Scratch
- Is it correct to have 2 methods from my repository running in the same controller ActionResult?
- Create report in MVC using LINQ
- Sum multiple lists of ints into one by grouping using LINQ
- How to select rows from DataTable based on Index / Row Number?
- C# : insert data into database using linq
- Select null in a linq query if the value is in a previous record
- LINQ to SQL: why a primary key is necessary for INotifyPropertyChanged?
- use linq to query a cell from datatable
- Should GetHashCode() method take care about null value given as parameter?
- XNamespace url is written in XAttribute when I create my XML
- Linq FirstOrDefault
- Getting an element tree broken down from an XML