ienumerable

Should Count() of an IEnumerable be avoided?

╄→гoц情女王★ 提交于 2021-02-18 20:29:32
问题 In general, I am using a List and then returning them as IEnumerable when I no longer need to update them. However, I ran into an issue where I actually need to enumerate through them but first need to know the count. Will IEnumerable enumerate every item and find the count (O(N)), or will it rely on List 's Count property (O(1))? Also, what if the IEnumerable is the result of a LINQ query? 回答1: Will IEnumerable enumerate every item and find the count (O(N)), or will it rely on List's Count

Adding an item to IEnumerable

天大地大妈咪最大 提交于 2021-02-18 11:20:09
问题 I had a List<> of my objects, but now need to change it to an IEnumerable<>. So, I have this: public IEnumerable<TransactionSplitLine> TransactionSplitLines { get; set; } However, I can no longer do: reply.TransactionSplitLines.Add(new TransactionSplitLine {Amount = "100", Category = "Test", SubCategory = "Test More", CategoryId=int.Parse(c)}); How should I be adding items now? 回答1: You could do something like the following, using Concat: reply.TransactionSplitLines = reply

Aggregate and ToArray function in System.Array not working

≡放荡痞女 提交于 2021-02-17 05:24:09
问题 I have this two errors: 'System.Array' does not contain a definition for 'Aggregate' and no extension method 'Aggregate' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) 'System.Collections.Generic.IEnumerable<object[]>' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Collections.Generic.IEnumerable<object[]>' could be found (are you missing a

Enumerator.MoveNext() throws 'Collection was Modified' on first call

夙愿已清 提交于 2021-02-16 13:34:53
问题 Consider the following code: List<int> list = new List<int>(); IEnumerable<int> enumerable = list; IEnumerator<int> enumerator = enumerable.GetEnumerator(); list.Add(1); bool any = enumerator.MoveNext(); At runtime, the last line throws an: InvalidOperationException: Collection was modified; enumeration operation may not execute. I understand the need for IEnumerators to throw 'Collection was modified' exceptions when the IEnumerable changes, but I don't understand this: Why does the

Enumerator.MoveNext() throws 'Collection was Modified' on first call

坚强是说给别人听的谎言 提交于 2021-02-16 13:33:22
问题 Consider the following code: List<int> list = new List<int>(); IEnumerable<int> enumerable = list; IEnumerator<int> enumerator = enumerable.GetEnumerator(); list.Add(1); bool any = enumerator.MoveNext(); At runtime, the last line throws an: InvalidOperationException: Collection was modified; enumeration operation may not execute. I understand the need for IEnumerators to throw 'Collection was modified' exceptions when the IEnumerable changes, but I don't understand this: Why does the

Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class

社会主义新天地 提交于 2021-02-11 15:00:31
问题 I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below private void LoadVariables(LogDictionary dic) { foreach (var entry in dic) { _context.Variables[entry.Key] = entry.Value; } } but a NotImplementedException is thrown because I did not implement the GetEnumerator() method. Here is my wrapper class: public class LogDictionary: IDictionary<String, object> { DynamicTableEntity _dte; public LogDictionary(DynamicTableEntity dte) { _dte = dte;

Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class

ⅰ亾dé卋堺 提交于 2021-02-11 14:58:30
问题 I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below private void LoadVariables(LogDictionary dic) { foreach (var entry in dic) { _context.Variables[entry.Key] = entry.Value; } } but a NotImplementedException is thrown because I did not implement the GetEnumerator() method. Here is my wrapper class: public class LogDictionary: IDictionary<String, object> { DynamicTableEntity _dte; public LogDictionary(DynamicTableEntity dte) { _dte = dte;

How to get only specific field from the list

☆樱花仙子☆ 提交于 2021-02-08 13:34:06
问题 I have an IEnumerable of Lesson objects: IEnumerable<Lesson> filteredLessons I convert it to a List through the following method: ToList(); But I want the returned list to contain only the first property, lessonid , not all the Lesson properties. How can I get the data of specific property of the list instead of the objects? 回答1: You can select the value you want first, like this: filteredLessons.Select(l => l.lessonId).ToList(); And you'll get a list of ID's 回答2: If you want to get the the

Use Contains() extension method on arrays in C#

不羁岁月 提交于 2021-01-29 03:31:26
问题 There is a Contains() extension method on IEnumerable; In VB I am able to do this: If New String() {"A", "B"}.Contains("B") Then ' ... End If What would be the equivalent of this in C#? 回答1: It is the same idea in C#, using LINQ extension methods: using System.Linq; ... if (new string[] {"A", "B"}.Contains("B")) { ... } 来源: https://stackoverflow.com/questions/1972820/use-contains-extension-method-on-arrays-in-c-sharp

How to validate yield return default in IEnumerable?

时间秒杀一切 提交于 2021-01-28 23:11:30
问题 I am trying to check whether IEnumerable<> is null or empty but somehow my if check always fails whenever it is empty. private bool Update() { IEnumerable<RecordHolder> recordHolders = GetData(); // below check doesn't work if (recordHolders == null || !recordHolders.Any()) return false; // .. some other code } public IEnumerable<RecordHolder> GetData() { var isSuccess = PullRemote(url); if (!isSuccess.Result) { yield return default; } // .. some other code } Whenever my GetData() method