score:2
I'd say that the LINQ operators already work on general sequences - but they're not designed to work specifically for monotonic sequences, which is what you've got here.
It wouldn't be too hard to write such things, I suspect - but I don't believe anything's built-in; there isn't even anything for this scenario in System.Interactive, as far as I can see.
score:1
You may onsider the Seq module of F#, which is automatically invoked by using special F# language constructs like 1 .. 10
which generates a sequence. It supports infinite sequences the way you describe, because it allows for lazy evaluation. Using F# may or may not be trivial in your situation. However, it shouldn't be too hard to use the Seq module directly from C# (but I haven't tried it myself).
Following this Mandelbrot example shows a way to use infinite sequences with C# by hiding yield
. Not sure it brings you closer to what you want, but it might help.
EDIT
While you already commented that it isn't worthwhile in your current project and accepted an answer to your question, I was intrigued by the idea and conjured up a little example.
It appeared to be rather trivial and works well in C# with .NET 3.5 and .NET 4.0, by simple including FSharp.Core.dll (download it for .NET 3.5) to your references. Here's an out-of-the box example of an infinite sequence implementing your first use-case:
// place in your using-section:
using Microsoft.FSharp.Collections;
using Microsoft.FSharp.Core;
// [...]
// trivial 1st and 15th of the month filter, starting Jan 1, 2010.
Func<int, DateTime> firstAndFifteenth = (int i) =>
{
int year = i / 24 + 2010;
int day = i % 2 != 0 ? 15 : 1;
int month = ((int)i / 2) % 12 + 1;
return new DateTime(year, month, day);
};
// convert func to keep F# happy
var fsharpFunc = FSharpFunc<int, DateTime>.FromConverter(
new Converter<int, DateTime>(firstAndFifteenth));
// infinite sequence, returns IEnumerable
var infSeq = SeqModule.InitializeInfinite<DateTime>(fsharpFunc);
// first 100 dates
foreach (var dt in infSeq.Take(100))
Debug.WriteLine("Date is now: {0:MM-dd-yyy}", dt);
Output is as can be expected, first few lines like so:
Date is now: 01-01-2010 Date is now: 01-15-2010 Date is now: 02-01-2010 Date is now: 02-15-2010 Date is now: 03-01-2010
Source: stackoverflow.com
Related Articles
- LINQ - Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator
- Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator
- LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Is there a VB.NET-Like operator in C#?
- Is there a good source that gives an overview of linq optimizations?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Is there a way to speed up this code that finds data changes in two XML files?
- Is there a way to determine whether IEnumerable<T> is a sequence generator or true collection?
- Is there any open source software for converting SQL statements to LINQ?
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- Is there any default order in which where clause in LINQ query filters sequence
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- creating Linq to sqlite dbml from DbLinq source code
- Are there any production-ready LINQ implementations for javascript?
- GroupByUntilChanged: is there a LINQ GroupBy operator that groups by SUCCESSIVE equal keys?
- Is there any way to make Code Contracts work with LINQ?
- Is there a LINQ alternative operator to Where() operator with List.Contains() method inside
- Are there sequence-operator implementations in .NET 4.0?
- Is there any way to map a ISO Currency Code back to a culture string?
- Linq query: where condition yields an empty sequence even if there should be one match
- is there any linqish way to save user form data - Re factoring my code
- source code for LINQ 101 samples
- Is there any Operator "LIKE" in the Observable Collection for filetring
- Is there a better way to code this LINQ fragment?
- Is there a better way to code this Duplicate ID Checker?
- Create a linq subquery returns error "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator"
- Finding duplicate XML nodes and minimize XML strcutre and size
- Left/outer join with linq on c# with where condition clause
- Linq filtering entity's collection ends up in LINQ to Entities casting error
- Problem merging IEnumerable<T> using Lambda expressions
- How to include a table multiple times in Linq query
- Use inner join in linq
- Joining two lists in LINQ
- Linq to Object gives "Specified argument was out of the range of valid values."
- npgsql LINQ creates SQL Server sql syntax
- Linq: Count number of times a sub list appear in another list
- How to print all the values using linq?
- Entity framework. How to right map tables without primary key
- || (OR) operator in LINQ translated into AND in SQL script
- Convert string to int for ordering using LINQ
- Why LINQ operator can't add statement block?
- Converting SQL to LINQ to hit database once
- Why can't I use a dictionary with Entity Framework
- how to Group By LINQ Query result to DataGridView without looping
- Create XElement from List<T> where T.property is null
- Get all data from database for Administrator role but not for other roles, using same query