Using Ienumerable Without Foreach Loop

Using IEnumerable without foreach loop

You can get a reference to the Enumerator, using the GetEnumerator method, then you can use the MoveNext() method to move on, and use the Current property to access your elements:

var enumerator = getInt().GetEnumerator();
while(enumerator.MoveNext())
{
int n = enumerator.Current;
Console.WriteLine(n);
}

How to modify IEnumerable with LINQ to avoid foreach loop?

Reassign the list, ideally without mutating anything. This would be more consistent with the functional programming mindset of LINQ.

IEnumerable<MyObj> myList1 = EF.GetList(etc);
IEnumerable<MyObj> myList2 = myList1.Select( x =>
new MyObj
{
uploadedAt = MyUtils.ConvertToLocalTime(x.uploadedAtUTC, x.clientTimezone),
anyOtherFields = x.anyOtherFields
}
).ToList();

If constructing a MyObj is complicated and/or you really need to mutate it, you can also do this:

IEnumerable<MyObj> myList = EF.GetList(etc);
myList = myList.Select( x => {
x.uploadedAt = MyUtils.ConvertToLocalTime(x.uploadedAtUTC, x.clientTimezone);
return x;
}).ToList();

But if you're going to do it that way, you may as well use the for loop.

How to loop through a collection that supports IEnumerable?

A regular for each will do:

foreach (var item in collection)
{
// do your stuff
}

Iterate through IEnumerable Model without loop - ASP.NET-MVC5

You can access it by it's Index if you use a List:

@model List<App.DAL.Model.EmergencyContact>
.............//other code

@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })

But since you are now accessing the object by it's index I would always recommend checking to see if it's null first, even know you say you will always have 2 items in the collection, it doesn't do any harm checking.

Update

You can check if it is null by doing the following in a Razer view:

@if(Model[0] != null) 
{
@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
}

Foreach on collection cast to IEnumerable work slower than without cast?

This is due to the difference between the call and callvirt instruction used.

call        System.Collections.Generic.List<UserQuery+Thing>+Enumerator.get_Current
call System.Collections.Generic.List<UserQuery+Thing>+Enumerator.MoveNext

vs

callvirt    System.Collections.Generic.IEnumerator<UserQuery+Thing>.get_Current
callvirt System.Collections.IEnumerator.MoveNext

The callvirt instruction does a null check, that is why it is slower.

Using IEnumerable with foreach loop in C#

_queries itself is enumerable (because it is a List<string> which implements IEnumerable<string>, so you can rewrite this to just be:

private void UpdateRecords(MySqlConnection connection) 
{
foreach (string query in _queries)
{
// process query
}
}

Foreach loop doesn't iterate over entire IEnumerable

An IEnumerable is an iterator, so it returns one result at a time.

Each time foreach loops, it asks the iterator for the next result. Sometimes results can disappear from the collection being enumerated, so you get unexpected behaviour.

To avoid this, make sure you have all the results before starting the foreach, by calling .ToList()

  // Make a list of all documents
List<Document> documentsToAdd;

documentsToAdd = dbEvent.Documents
.Where(dbd => !eventToSave.Documents
.Select(d => d.DocumentId)
.Contains(dbd.DocumentId))
.ToList(); // load all results

// Now this will loop through the whole list
foreach (Document documentToAdd in documentsToAdd)
{
documentToAdd.DocumentType = null;
documentToAdd.DeletedByUser = null;
documentToAdd.DocumentOwnerTeam = null;
documentToAdd.UploadedByUser = null;
documentToAdd.UploadedInStage = null;

hcDbContext.Documents.Add(documentToAdd);
}


Related Topics



Leave a reply



Submit