This question already has answers here:
How to get the first element of IEnumerable
(5 answers)
Closed 9 years ago.
If I have a generic IEnumerable<int>. I can simply apply ToList() or ToArray() or FirstOrDefault() to it. How to apply these methods to a non-generic IEnumerable?
You have couple of options:
If you know that all objects in your enumerable are of the same type, you can cast it to the generic IEnumerable<YourType>. In worst case you can always use object:
object first = enumerable.Cast<object>().First();
Or you can use enumerator, make one step and take current element:
IEnumerator enumerator = enumerable.GetEnumerator();
enumerator.MoveNext();
object first = enumerator.Current;
You have two options here:
Follow the question that #Danier Gimenez suggested and make use of the Cast<TResult> method. After the cast you get a generic enumerable on which you can apply the First() method. This is also the most simple implementation.
Use the GetEnumerator() method which gives you an IEnumerator. And from here you can iterate over the collection. Starting with MoveNext(), you can use the Current property to get the first element.
Edit:
Andrei was ahead of me.
it is simple
look this sample code
IEnumerable collection;
--- fill collection here---
collection.OfType().ToList() or collection.OfType().ToArray
collection.OfType().ToList() or collection.OfType().ToArray()
it's filter the (int/MyClass) types object and convert it to a list or array
Related
This question already has answers here:
Array versus List<T>: When to use which?
(16 answers)
Closed 3 years ago.
If some method requires an ICollection<T> as an argument, but I have only an IEnumerable<T> available: Is it better to convert this IEnumerable<T> to a IList<T> or is it better to convert it to an array T[] or shall I convert it to something else or is there no difference at all?
The IEnumerable<T> is only required for this method call, so no further read/write/extend is required.
If you're not going to be accessing the elements, you should use ToList() as explained why here.
If you'll be iterating through the elements, then it'd be better performance-wise to do it via an index (meaning casting them to an array), so that you can use a for loop, instead of a foreach loop.
for is faster than your typical foreach due to it using an index to access each element.
This question already has answers here:
Why can't I change elements from a linq IEnumerable in a for loop?
(3 answers)
Closed 7 years ago.
I'm stuck with next behaviour:
var contracts = result.Select(this.Map);
foreach (var contract in contracts) {
contract.Id = 1;
}
return contracts;
After loop contract.id were not updated and still have initial value.
var contracts = result.Select(this.Map).ToList(); solved my problem, but i can't figure out why couldn't I update properties of iterating variable?
You need to remember that the results of a LINQ query is an object representing the query, not the results of that query. contracts is an object that, when iterated, will perform a mapping of each object in the underlying collection to a new object. Each time it is iterated, it performs the mapping again.
In your case you're iterating the source collection, performing the mapping, editing those objects, throwing those objects on the floor, iterating the collection again, performing the mapping again, and then looking at those objects, so of course the edits you made aren't seen.
When you call ToList you're now no longer storing a query, but rather the results of that query, and edits to the items in that collection are observed, because you're looking at the same items in subsequent iterations.
This question already has answers here:
List<T> or IList<T> [closed]
(18 answers)
Closed 8 years ago.
Why here interface Ilist is used to create an object of List type
IList<BankAccountView> bankAccountViews = new List<BankAccountView>();
when it can be done like this
List<BankAccountView> bankAccountViews = new List<BankAccountView>();
A List is a concrete type, while an IList is a contract for which you can use any implemtation that an IList has.
An IList has a set of methods as defined on MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx
Here's a very good blogpost explaining it in detail: http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/
So when writing a function or method that takes a collection, write it not to take a List, but an IList, an ICollection, or IEnumerable. The generic interfaces will still work even for heterogenous lists because System.Object can be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you need to do in the function is foreach through it, IEnumerable is really all you should be asking for.
On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around. So in that case, if it's a List internally, return a copy as a List.
Read link: When to use IList and when to use List
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
Why ForEach extension method is not available in ObservableCollections class while it is available in List ?
As JJohn says, it's by design. Read this post by Eric Lippert for more information. A standard foreach loop works just as well and is generally more readable.
ForEach is available only in List<T> and not in any other collection. It is just design decision.
Foreach can be used only on those objects which are of classes that implements IEnumerable.
IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.
This question already has answers here:
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
Can anyone tell me when I should use either.
For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct?
Thank you.
Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList (IList implements IEnumerable) so unless you want something specific from IList (such as Count as you suggest, or perhaps Add, Delete, etc), I'd use IEnumerable.
One benefit of remaining with IEnumerable is that you can write iterator methods to return this type (look up "yield return" and iterator methods if you are not familiar with them). This allows you to write very memory efficient "pipelines" for your loops.
You use IEnumerable when you want to loop through the items in a collection.
IList is when you want to add, remove, and access the list contents out of order.
IList vs IEnumerable for Collections on Entities
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=722
You should use IList when you need access by index to your collection, add and delete elements, etc., and IEnumerable when you need just enumerate over your collection.
A very simple answer, I can extend it if you will describe you scenario.