Questions tagged [ienumerable]
IEnumerable, and its generic counterpart IEnumerable<T> are .NET interfaces for iterating (or enumerating) through a collection of items.
3,416
questions
1
vote
1
answer
55
views
Why is a LINQ query called every time a variable is accessed? [duplicate]
Why is Square() being called again in the code below? I suppose values is being recalculated, but why? We got the values earlier, why is it being called again? Why does the compiler issue a warning &...
-1
votes
1
answer
52
views
IENumerable is readonly.. but it isn't?
many topics on this question. Wondering why sometimes the collection is readonly, and sometimes not. I came across an issue in a netCore 3.1 project where the intention was to modify a collection ...
-1
votes
0
answers
37
views
Why foreach loop can't proceed IEnumerable collection? [duplicate]
I am doing a simple algorithm that takes from the first list elements that are not present in the second list and print them.
The code is giving an error, but when you explicitly call .ToList() that I ...
-1
votes
0
answers
44
views
How to group IEnumerable?
I have an ASP.NET razor page (view) that displays data in a series of rows. I am looking for a solution for grouping the IEnumerable objects into 3 columns per row using IndexOf. This is easier with ...
1
vote
1
answer
46
views
Check a list of members to find whether any match a given property (e.g. password)
My issue lies in this bit of code below.
It functions as expected for null or whitespace issue but after porting my staff member login details over to an SQLite db, I'm now struggling to correctly ...
0
votes
2
answers
42
views
How can I add an item to a IEnumerable<(T item, int? number)>?
So, I have method:
public static IEnumerable<(T item, int? number)> DoTheThing<T>(this IEnumerable<T> enumerable, int? someNumber)
Basically I need return collection from parameters ...
0
votes
0
answers
57
views
Error regarding 'IEnumerable<>' does not contain a definition for '' ....'' accepting a first argument of type 'IEnumerable<>' could be found
I'm facing the mentioned error for Username only, but not for password? I am confused as to what I could be doing wrong.
In index.cshtml:
<input id = "Username" asp-for="Username&...
2
votes
1
answer
49
views
gRPC: Turn a message proto into a List<message> (C# .Net6)
I want to turn a messageResponse from a protocol buffer into a
IEnumerable<messageResponse>
so in the service will return me an IEnumerable<messageResponse>
Proto I have:
syntax = "...
0
votes
1
answer
34
views
Where to IEnumerable that contains a list of objects
I new to EntityFrameworkCore and Asp.Net Core and I have the following problem.
I have 2 tables, Diplomas and Teachers and I am trying to select the diplomas that have as TeacherName the given one. I ...
1
vote
0
answers
42
views
TakeWhile filtering on <IEnumerable<IGrouping>> returns InvalidCastException
I have a private IEnumerable<IGrouping<string MyClass>> myGrouping;
And I am doing this with it:
foreach (IGrouping<string, MyClass> filteredGrouping in myGrouping.Select(x => x....
0
votes
1
answer
44
views
How to make a Deconstructor for class that implements IEnumerable
I have a class that implements IEnumerable. It contains a GetEnumerator() method and a private class that implements IEnumerator. The Current() method of that class returns an object that is a ...
0
votes
0
answers
37
views
Getting name of IEnumerable method
I have a method that takes a non-generic IEnumerable as a parameter, creates an Enumerator out of it, and iterates over it.
void Do(IEnumerable something)
{
foreach(var item in something)
{
...
0
votes
1
answer
65
views
How to change return type in LINQ query
I am searching for a good way to change the return type from
IEnumerable in anonymousType to IEnumerable<(string Category, decimal averagePrice)>
/// <summary>
/// Gets the average price ...
0
votes
0
answers
38
views
Iterate collection in expression
I need to iterate over collection in expression:
private readonly Expression<Func<TModel, IEnumerable<TMember>>> _collection;
private readonly IEnumerable<Expression<Func<...
-2
votes
2
answers
69
views
c# linq - aggregate values to a list property
Consider a hypothetical scenario where a huge used car dealer (e.g. CarMax) has many dealerships in different states.
public class Inventory
{
public string Make { get; set; }
public string ...
0
votes
2
answers
47
views
C# Generic foreach over IEnumerable of unknown type
I'm trying to write a generic static function that takes an instance of an IEnumerable class, the name of a property of and a string separator. It will loop through the instance and with each member ...
0
votes
2
answers
78
views
Adding objects to IEnumerable
Im making a basic CSV Reader. Im sepparating the header from the contents using header and data.
Now, my lists contain data of the type person.:
public class person
{
public int id;
...
-1
votes
2
answers
85
views
How to return multiple keyvalue pairs in c# using foreach loop? [duplicate]
Currently I have written a logic to return multiple key value pairs using for each loop, but it's return just the first key-value pair.
My current code is:
public static string ReturnData(IEnumerable&...
0
votes
1
answer
101
views
Convert IEnumerable to Collection (not ICollection)
Linq returns IEnumerable and the code has a Collection. How to assign the return value of a Linq operation to a Collection variable?
using System;
using System.Collections.Generic;
using System....
0
votes
2
answers
31
views
XML Linq - find a value of XElement from nested Descendants
I have a sample of the following XML response from a REST call
<GetHelloWorldResponse xmlns="http://www.helloworld.com/Services/HelloWorld">
<Available xmlns:i="http://www.w3....
0
votes
1
answer
198
views
Unable to cast object of type 'Collections.Generic.List`1[Microsoft.EntityFrameworkCore.Query.IIncludableQueryable`2 to type Generic.ICollection
I am trying to add a property of a class from a query. I have two classes "Article & Source with manytomany relationship.
source:
public class Source
{
public Source()
{
...
0
votes
1
answer
69
views
how to convert a model object to an IEnumerable
i have the following query that gets data regarding a vessel as follows
public async Task<ExcelListModel>HandleAsync(VesselCrewQuery query)
{
var test= await _queryDispatcher....
1
vote
0
answers
65
views
How to implement IEnumerable in C# using generics
My code has an iterator inner class which inherits from IEnumerator interface and Vector outer class which inherits from IEnumerable and IComparable interfaces.
public class Vector<T> : ...
0
votes
1
answer
13
views
How would you create a List<string> out of an enum without following the enum structure of having 'condensed' naming policies?
With an enum like say:
public enum Authors
{
KirkPatrick,
JasmineLliki,
NatashaKakuvi
}
Now using this helper method I can easily get the corresponding List<string> values:
public class ...
0
votes
1
answer
88
views
C# Passing array with specific array index
I want to pass an array to a function with specific index in C#, like we do in C++, something like below:
void foo(int* B) {
// do something;
}
int main() {
int A[10];
foo(A + 3);
}
In this case ...
0
votes
1
answer
102
views
C#. Given an Enumerator, iterate till end to produce an enumerable
In C#, I would like to write an extension method that would take an Enumerator<T> and give back an IEnumerable<T>. This could prove useful, for example, if I had to enumerate to some point ...
0
votes
0
answers
36
views
.NET: Simple way to test if an IEnumerable is infinite, or prohibit infinite collections
I saw the post on ways to handle an infinite IEnumerable, but I didn't see a good answer for testing if an enumerable is infinite.
My end goal is to write a function for all collection types in .NET, ...
0
votes
1
answer
47
views
LINQ for untyped IEnumerable
I am getting the e object via Reflection and then, if it is an IEnumerable, dump it to string:
public static string EnumerableToString(this IEnumerable e, string separator)
{
List<string> ...
0
votes
1
answer
48
views
Issue when using DataTable.Select(somestring) versus DataTable.AsEnumerable().Where
I am trying to convert an old code which is preparing a where clause first and call select on DataTable, to a chaining method using AsEnumerable(). But my code does not produce the same result. Any ...
0
votes
0
answers
60
views
Join two string lists elements one by one
I have two IEnumerable<string> that I need to combine joining their elements "index by index".
At the moment I'm using a simple foreach statement to merge the elements one by one and ...
0
votes
1
answer
51
views
LINQ with groupby and aggregate always shows default value
I have 2 model objects, DriverDetailsSourceObject which is meant to be for holding the DriverDetailsAPI response and DriverDetailsView which is used to show DriverDetails details with aggregated ...
-1
votes
1
answer
65
views
The IEnumerable appraoch pipeline
code from https://ayende.com/blog/3082/pipes-and-filters-the-ienumerable-appraoch
Question
There is registered 3 operations.
First gets all Processes in system.
Second filters processes.
Third writes ...
0
votes
0
answers
34
views
How can I avoid IEnumerable nested in another IEnumerable using lambda expressions
I am refactoring some code that currently consists of a foreach inside another foreach to get a list of types. Later on on those types some action is performed.
At the moment I end with an IEnumerable ...
-1
votes
1
answer
71
views
Map an Entity iEnumerator To Dto Enumerator
I am using CQRS. I select my Entities IEnumerator from database and i want to map this to my Dto class.
My Dto class:
public class XCollectionDto
{
public IEnumerable<XReadDto> Entries { get;...
2
votes
1
answer
57
views
Having IEnumerable<T> declared inside interface while having IList<T> declared in concrete class
Consider an example:
I have an interface like this. I need this interface to be covariant - meaning I have to declare my generic as "out" and use IEnumerable instead of list - and that's all....
1
vote
1
answer
106
views
dependency injection c# get ienumerable new instance every time
I have a list of class that implement an interface, like this:
pulic interface ISample
{
public int Id { get; set; }
}
pulic class SampleA : ISample {}
pulic class SampleB : ISample {}
pulic class ...
-1
votes
1
answer
81
views
JsonConvert a single delimited string to List<TEnum>
I am trying to read and write from a delimited string to a List of enums. But the annotated converter fails silently (returns null).
I would also like to reuse the converter code in the constructor ...
0
votes
1
answer
57
views
How do I merge non-generic IEnumerable in C#
I'm trying to have one object inherit from another object where properties of type IEnumerable will be merged together.
Both objects are of same class.
foreach (var property in typeof(Placeholder)....
1
vote
1
answer
42
views
Why debug and test skips over a method returning IEnumerable<t> in C#? [duplicate]
Recently, I tried and failed so many times with xUnit test with the below code :
// testing target
IEnumerable<T> Foo(int n)
{
if(n==0) throw new AnException();
foreach(var item in ..)
...
0
votes
1
answer
100
views
C# Linq query to join two lists and assign the result to new list
First model:
public class Group
{
public string Name { get; set; }
public string Sid { get; set; }
}
Second model:
public class GuidelinesWorkTeam
{
public int Id { get; set; }
...
0
votes
1
answer
57
views
Can an Order be guranteed if return type is IEnumerable?
I am currently working on refactoring some code, and came across this method.
public IEnumerable<KeyValuePair<string, string>> GetDataItems()
which does this
var data = ...
-1
votes
1
answer
69
views
NullReferenceException in Image Upload with C# web API and Reactjs
I am trying to upload multiple images at once and saving the image names by comma-separated values in the Database. But there is an exception thrown right after I call the API endpoint for uploading ...
1
vote
1
answer
39
views
Using IEnumerable.Repeat to populate a counter
I want to create an IEnumerable list using Enumerable.Repeat and would like the set the Id of the objects to the nth element of the list as per the code below. Is this possible with Enumerable.Repeat ...
0
votes
1
answer
79
views
Filter a list based on another list containing IEnumerable<Guid> using linq
I am looking for a way to filter a list based on another list that contains only the ids. If List A does not contain any of the ids in listOfGuids, get the filtered List that does not contain the ids
...
0
votes
1
answer
97
views
How to speed up foreach loop for IEnumerator
Little background
I am trying to use Intersystems IRIS Api
In case you want to see it.
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=BNETNAT_refapi#BNETNAT_refapi_iris-...
3
votes
2
answers
102
views
IEnumerable<T> from Enumerable.FromRange().Select() vs ToList()
This really stumped me, as I expected 'pass by reference' behavior. I expected this code to print "5,5,5", but instead it prints "7,7,7".
IEnumerable<MyObj> list = ...
0
votes
1
answer
124
views
WPF c# onStartUp() view model problem with multiple data inputs
Hey all I have the following OnStartup for my WPF desktop app that goes to a local directory and gathers all the images within that folder (box1 being the example code below):
public partial class App ...
2
votes
2
answers
188
views
Performance of Enumerable Count method in a loop
I have a sequence of items represented by IEnumerable
I need to loop thru these items, and It should be a for-loop because the index is important.
My question is, is there a difference in performance ...
0
votes
1
answer
82
views
How to use IEnumerable instead of List?
I wanted to implement list of transactions and add them to a model with a query like this:
My controller:
List<Transaction> transactionLists = new List<Transaction>();
using (var ...
0
votes
0
answers
81
views
Filtering with Where statement doesnt work
I have an IEnumerable<DataRow> myObjects which I would like to filter with a Where statement as follows:
myObjects = myObjects.Where(o => o["someVar"] != true)
...but I cant get it ...