score:4
a linq query is really nothing other than a method call. as such, you can't "reassign" the object to which you've already called a method after it's been setup.
you could simulate this by creating a wrapper class that would allow you to change what actually gets enumerated, ie:
public class mutatingsource<t> : ienumerable<t>
{
public mutatingsource(ienumerable<t> originalsource)
{
this.source = originalsource;
}
public ienumerable<t> source { get; set; }
ienumerator ienumerable.getenumerator()
{
return this.getenumerator();
}
public ienumerator<t> getenumerator()
{
return source.getenumerator();
}
}
this would allow you to create the query, then "change your mind" after the fact, ie:
var somestrings = new list<string> { "one", "two", "three" };
var source = new mutatingsource<string>(somestrings);
// build the query
var query = source.where(i => i.length < 4);
source.source = new[] {"foo", "bar", "baz", "uh oh"};
foreach(var item in query)
console.writeline(item);
this will print foo
, bar
, and baz
(from the "changed" source items).
edit in response to comments/edit:
i guess one parallel that i'm (maybe erroneously) making is with regular expression: one can compile a regular expression and then apply it to any string. i'd like to do the same with queries and dictionaries.
a query is not like a regular expression, in this case. the query is translated directly into a series of method calls, which will only work against the underlying source. as such, you can't change that source (there isn't a "query object", just the return value of a method call).
a better approach would be to move the query into a method, ie:
ienumerable<tkey> querydictionary<tkey,tvalue>(idictionary<tkey,tvalue> dictionary)
{
var keys = from entry in dictionary
where entry.value < 5
select entry.key;
return keys;
}
you could then use this as needed:
var query1 = querydictionary(d1);
var query2 = querydictionary(d2);
Source: stackoverflow.com
Related Query
- How to swap the data source associated with a Linq query?
- How to query values associated with the foreign key using linq and display in listview
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How does linq actually execute the code to retrieve data from the data source?
- How can I write the following code more elegantly using LINQ query syntax?
- How to build a nested query with the dynamic LINQ library
- How do I write a LINQ query that inverts the grouping of a hierarchical data source?
- How to get the value of class properties with Linq query and IEnumerable
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- How can I make a LINQ query with subqueries in the from statement?
- How do I optimize this Linq query to find blog posts with the most views in past 24 hours
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How do I determine the source of unpredictable LINQ query results?
- LINQ - database query using "into", how do I reference data prior to "into" in the select clause?
- How to write aggregate query in LINQ reusing part of the select code
- How to execute code as a part of the LINQ query
- Group in Linq with Max: How do I get the full data set of the max value?
- how to write a Linq query with a EF code first Many to Many relationship
- How do I write a LINQ query which will generate a JSON string with the following format?
- How to compare array of elements with the linq query result in c#
- How to query XML with the same element and attribute name using linq
- how to get data in the class format with the Linq
- How to create a Linq query to check if data exists in the database?
- How can I query a database with LINQ when the column is a number but I have a string in the WHERE?
- How to substitute the LINQ Query with lambda expression
- Unable to get linq query data in the async ActionResult with await and task, Error: await can't be used with async method
- How to use Linq Aggregate Query to print line sequence number alongwith some data from the List<string>?
- How I get the right Data from my Column with Linq
- Using LINQ and Entity Framework, how can I just populate some properties of a Dbo with data from the DB?
- How to run linq query with select when a primary key column returns null and thus the related attribute is null
More Query from same tag
- LINQ Lambda where in subquery
- What is the Linq.First equivalent in PowerShell?
- C# linq Perform join with default value if join returns null
- Grabbing text of a single element in XDocument (C#)
- Populating textboxes using LINQ2SQL
- Entity Framework Query for Complex Sql
- How to make combination based on limit provided?
- How to Search in List<string>
- Group elements based on two parameters
- Cant bind complex property to gridview with entity framework
- iqueryable select/where not working
- How to get the value of an XML element using Linq even when empty
- In LINQ, does orderby() execute the comparing function only once or execute it whenever needed?
- In Memory Data Cache for Performance in .Net Applications
- Can't converting XElement to decimal
- How does LINQ sort items by a given criteria?
- Receive index of current comparison in a linq statement
- How to do partial search on multiple keys dictionary
- Issue With LINQ and Reference List
- C# linq group to a list inside a list
- Get two linq queries into one listbox
- I need a specific decimal conversion I can not do that
- Recursive LINQ calls
- How to add List<T> containing item of type List<string> ,int,string into another List
- Edit Object with xml without creating new Instance
- Inserting particular XElement at specific position in a XML using LINQ
- Linq Query Returns Records Including Blank String Data
- ASP.NET Web API GET Method: Passing multiple values for single parameter
- ASP.Net LINQ data source error is ListView
- System.Linq.Dynamic.Core cannot cast 'object' type to 'Tuple<T1, T2, T3>'