still trying to find where i would use the "yield" keyword in a real situation.
I see this thread on the subject
http://stackoverflow.com/questions/39476/what-is-the-yield-keyword-used-for-in-c
but in the accepted answer, they have this as an example where someone is iterating around Integers()
public IEnumerable<int> Integers()
{
y...
I have been testing out the yield return statement with some of the code I have been writing. I have two methods:
public static IEnumerable<String> MyYieldCollection {
get
{
wrapper.RunCommand("Fetch First From Water_Mains");
for (int row = 0; row < tabinfo.GetNumberOfRows() ; row++) //GetNumber...
Many people ask me why, and I don`t have a good answer for them.
Obviously there is a good reason. Does anyone know it?
I searched here and found this question. It explains how it works, but not why.
...
Duplicate
Modifying A Collection While Iterating Through It
Has anyone a nice pattern to allow me to get around the inability to remove objects while I loop through an enumerable collection (eg, an IList or KeyValuePairs in a dictionary)
For example, the following fails, as it modifies the List being enumerated over during the forea...
The code below is checking performance of three different ways to do same solution.
public static void Main(string[] args)
{
// for loop
{
Stopwatch sw = Stopwatch.StartNew();
int accumulator = 0;
for (int i = 1; i <= 100000000; ++i)
{
accumulator +...
int[] mylist = { 2, 4, 5 };
IEnumerable<int> list1 = mylist;
list1.ToList().Add(1);
// why 1 does not get addedto list1??
...
I'm writing a program as follows:
Find all files with the correct extension in a given directory
Foreach, find all occurrences of a given string in those files
Print each line
I'd like to write this in a functional way, as a series of generator functions (things that call yield return and only return one item at a time lazily-loaded)...
I've read a couple of blog posts mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List?
Thanks!
...
hi everyone,
i am trying to use this code to bind my asp.net menu control to a collection..
but its giving me an error that my collection is now IHierarchyEnumerable.. which I understand why too..
StringCollection sc = pos.getAllmembers();
Menu1.DataSource = pos.getAllmembers().GetEnumerator();
is there a way around this.....
Hi!
Here's what I'm trying to do. I'm querying an XML file using LINQ to XML, which gives me and IEnumerable<T> object, where T is my "Village" class, filled with the results of this query. Some results are duplicated, so I would like to perform a Distinct() on the IEnumerable object, like so:
public IEnumerable<Village> GetAllAlliance...
I don't understand why IList implements IEnumerable taking in consideration that IList implements ICollection that implements IEnumerable also.
...
I would like to call FindLast on a collection which implements IEnumarable, but FindLast is only available for List. What is the best solution?
...
I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the other?
IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator();
while(classesEnum.M...
Ok, I have managed to get the following working
public IQueryable getTicketInformation(int ticketID)
{
var ticketDetails = from tickets in _context.tickets
join file in _context.file_objects on tickets.ticket_id equals file.source_id
where tickets.ticket_id == ticketID
select new { tickets.ticket_id, tickets....
How can I convert a List to an IEnumerable and back again.
The reason I want to do this is to run a series of LINQ statements on the List i.e. sort etc
...
Hi,
Say i need to filter a generic list with a dynamic query (List<string> l; var x = l.Where(*dynamic query*))
How would i do this using LINQ? (Currently using a row filter on a dataview)
I've seen a posting by scott g: but it appears to not work with objects that use IEnumerable (generic lists included)
Can anyone offer any ideas?...
Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection?
class Bar
{
private ICollection<Foo> foos;
// Which one is to be preferred?
public IEnumerable<Foo> Foos { ... }
public ReadOnlyCollection<Foo> Foos { ... }
}
/...
What is the best way to convert from a generic IEnumerable object to an array of the same type? The current solution I have looks like the following:
IEnumerable<string> foo = getFoo();
string[] bar = new List<string>(foo).ToArray();
The transfer through a List<T> seems unneccesary, but I haven't been able to find a better way to do ...
In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree.
Now if I want to mock my repository in theorie this is very easy : just implement the interface of my repository (which is also a IQueryable object).
My mock repository implementation would be only a in memory co...
Since there is no Linq to DB2 yet (c'mon IBM!), and I want to deal with IQueryables or IEnumerables in my code, how would I convert a DataTable to an IQueryable? Or an IEnumerable?
I have an interface and a class that matches the columns in the datatable...
IQueryable<IMyData> GetAS400Data(..parameters..)
{
DataSet d = GetData();
...