score:1
Like so? I haven't really changed much here, except the constructor usage...
static void Main()
{
var q = from d in System.Linq.Enumerable.Range(0, 10)
select new Disposable(d);
// alternatively:
// var q = System.Linq.Enumerable.Range(0, 10)
// .Select(d => new Disposable(d));
foreach(var item in q)
{
Console.WriteLine("Hi");
}
}
public static IEnumerable<R> Select<T, R>(
this IEnumerable<T> list, Func<T, R> selector) where R : IDisposable
{
foreach (var item in list)
using (var disposable = selector(item))
yield return disposable;
}
However! The thing to watch here is that we don't introduce any conflicting LINQ operations.
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- Disposing an IDisposable in Linq
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- 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
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- How does linq actually execute the code to retrieve data from the data source?
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- How can I combine this code into one or two LINQ queries?
More Query from same tag
- LINQ - Where a list contains an element of another list
- Linq method syntax to flatten xml nodes
- Linq - how to replace a lists property with value from another list where both keys are equal
- how to speed up linq query on csv imported list
- Select element in linq and change properties
- C# linq group to a list inside a list
- Linq Select causes InvalidCastException
- ef core linq filtered child entities
- c# LINQ to XML test data loading
- Calculation did not perform to calculate the difference in total days between 2 dates in c#asp.net sql linq
- Complex DTO to use in a View Model with MVC
- Linq on string array
- How to delete a single record from a list of records stored in a session
- Reading Element Value from xml message with multiple namespaces
- Nested foreach loop to linq query
- Filtering a Collection using Linq based on Combo boxes Selections
- LINQ retrieving a record based on date
- Simplifying LINQ Query
- How to find a specific element in a List
- Mimic PHP's ksort in C#
- What is the easiest way to return the first set of linq query properties as optional strings?
- Linq AND searches
- Selecting max in linq query or rewriting to method chain syntax
- NHibernate query throws exception, when looking by related table
- Getting Time Series for a certain item with LINQ
- .Distinct() clause not working c# MVC
- Translate SQL to lambda LINQ with GroupBy and Average
- dynamically create lambdas expressions + linq + OrderByDescending
- Filtering a list by a property matching a property in another list
- LINQ results change at end of for loop