score:3
The IQueryable
most likely does have a reference to the underlying collection (possibly through a number of layers of indirection), but it won't be publicly exposed, so you won't be able to access it, at least not in any way that I would consider reasonable and not a very, very messy hack.
score:0
No, each query is executed independently, and all you're left with is the result set of the query itself. There is nothing within the memory object that contains the original collection nor any accessible reference to the original collection. Considering that the original collection may now be null or out of scope, this would be dangerous to allow even. If you need to the original collection, you will need to keep it in scope until you're completely finished with it.
score:0
This is cheating, but maybe it makes sense in your context:
interface IReferencedQueryable<T> : IQueryable<T>
{
IEnumerable<T> Source { get; }
}
static class IReferencedQueryableExtensions
{
public static IReferencedQueryable<T> AsReferencedQueryable<T>(
this IEnumerable<T> source)
{
return ReferencedQueryable.From(source);
}
class ReferencedQueryable<T> : IReferencedQueryable<T>
{
public IEnumerable<T> Source { get; private set; }
ReferencedQueryable(IEnumerable<T> source)
{
Source = source;
}
static IReferencedQueryable<T> From(IEnumerable<T> source)
{
return new ReferencedQueryable(source);
}
// all the IQueryable members would be
// implemented through AsQueryable()
// ...
}
public static IReferencedQueryable<T> Where<T>(
this IReferencedQueryable<T> source,
Expression<Func<T, bool>> predicate)
{
return ReferencedQueryable.From(
((IQueryable<T>)source).Where(predicate));
}
// do the same for all the Linq extension methos you want to support
}
You would use it like this:
T[] itemArray = // initialized values
var itemQuery = itemArray.AsReferencedQueryable()
.Where(*/some query*/)
.Skip(5)
.Etc() ...
When you need your source sequence you can access it through the Source
member, possibly through an as
cast.
var s = itemQuery.Source;
Source: stackoverflow.com
Related Articles
- creating Linq to sqlite dbml from DbLinq source code
- How to get original collection from an IQueryable<T>
- c# Linq or code to extract groups from a single list of source data
- Find a certain value from a source collection in a target collection of different type with linq
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- EF 4: Removing child object from collection does not delete it - why?
- linq select items from child collection
- Entity Framework recursively include collection for each entity from included collection
- How do I get the latest date from a collection of objects using LINQ?
- Linq to update a collection with values from another collection?
- Find And Remove Items From Collection
- Linq: How to query items from a collection until the sum reaches a certain value
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Accessing Results View from variable in code
- Generating the Shortest Regex Dynamically from a source List of Strings
- c# - LINQ select from Collection
- Select object from nested collection using Linq
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
- is there anyway to remove from a collection based on a .Where() linq clause (.RemoveWhere() ?)
- How can I add elements from one collection that do not exist in a second collection to a third collection?
- Using LINQ to find all keys from one collection that are not in another?
- Dynamically build lambda expression from a collection of objects?
- Return best fit item from collection in C# 3.5 in just a line or two
- using LINQ how can i concatenate string properties from itesm in a collection
- Returning the middle n (values not index) from a collection
- How to pass LinQ Expressions from F# to C# code
- LINQ query to create a collection of objects combined from a collection of strings
- EF Core group by date and count value for previous dates
- Is it possible to use Enumerable.Sum() extension with dynamic types?
- Why a .Union() changes the order of the items?
- Is there a pretty way to split in IEnumerable into 2 groups like this?
- Is there a plug-in for Visual Studio 2010 to query XML file using LINQ/XPATH?
- C# LINQ: Efficient way to set anonymous type property based on other values in query?
- Linq to Entity Query takes more than 2 minutes to execute
- Lambda expression C# failed to convert to the object type and fail with ref
- C# printing LINQ-table to console without specifying the properties
- C# Expressions: How is this AutoMapper code working?
- advanced grouping and aggregation with linq
- Linq to get immediate children in Nested Set Model
- Entity Framework Query Optimization
- How to return Generic.List<Anonymoustype> from a function in C#
- How to get upper bounds in Enumerable.Range
- Where clause in LINQ query is not recognized in vb.net?
- Rowspan in the view (mvc4)
- How to show 2nd row on Data in Next Button Click in C# and XML database using LINQ
- IEnumerable as DataTable performance issue
- Optimizing LINQ routines