ienumerable

How yield return works in c#

泪湿孤枕 提交于 2019-12-23 12:28:39
问题 I have following piece of code. public static void main(string []args) { var intergers = GetCollection(); foreach(var val in intergers) { console.writeline(val); } } public IEnumerable<int> GetCollection() { yield return 10; var obj = new MyClass(); obj.performLargeAction(); yield return 1; var obj = new MyClass(); obj.perform(); yield return 2; console.writeline("I am finished now"); } Now, when I debug the code and see the foreach iteration, then I see that first time method executes till

List, array and IEnumerable covariance

萝らか妹 提交于 2019-12-23 09:45:57
问题 I'll start with several postulates to better explain the context of my question: Array Covariance Postulate 1.1 An array of a value type is not covariant. int[] cannot pass for object[] . Postulate 1.2 An array of a reference type is covariant with a valid IEnumerable . string[] can pass for IEnumerable<object> ). Postulate 1.3 An array of a reference type is covariant with a valid covariant array. string[] can pass for object[] . List Covariance Postulate 2.1 (same as 1.1) A list of a value

What's the point of Enumerable.ElementAt<TSource>?

青春壹個敷衍的年華 提交于 2019-12-23 09:28:03
问题 IEnumerable<T> exposes an enumerator, so the object can be enumerated. There is nothing about indexes exposed by this interface. IList<T> is about indexes, as it exposes the IndexOf method. So what's the point of Enumerable.ElementAt? I just read the doc of this LINQ extension method: Returns the element at a specified index in a sequence. Well, yes, it's about a sequence , not just an IEnumerable . Reading the remarks: If the type of source implements IList, that implementation is used to

Update object in IEnumerable<> not updating?

吃可爱长大的小学妹 提交于 2019-12-23 09:27:59
问题 I have an IEnumerable of a POCO type containing around 80,000 rows and a db table (L2E/EF4) containing a subset of rows where there was a "an error/a difference" (about 5000 rows, but often repeated to give about 150 distinct entries) The following code gets the distinct VSACode's "in error" and then attempts to update the complete result set, updating JUST the rows that match...but it doesn't work! var vsaCodes = (from g in db.GLDIFFLs select g.VSACode) .Distinct(); foreach (var code in

Changes to an IEnumerable are not being kept between queries

限于喜欢 提交于 2019-12-23 09:02:22
问题 IEnumerable is a query that is lazily evaluated. But apparently my understanding is a bit flawed. I'd expect the following to work: // e.Result is JSON from a server JObject data = JObject.Parse(e.Result); JsonSerializer serializer = new JsonSerializer(); // LINQ query to transform the JSON into Story objects var stories = data["nodes"].Select( obj => obj["node"]).Select( storyData => storyOfJson(serializer, storyData)); // set a value on each story returned by the query foreach (Story story

In C# what is the meaning of 1 after IEnumerable in System.Collections.Generic.IEnumerable`1

我的未来我决定 提交于 2019-12-23 07:58:07
问题 What is the meaning of 1 after IEnumerable in: System.Collections.Generic.IEnumerable`1 回答1: It is the generic arity of the type, or put another way, the number of type parameters a generic type supports. IEnumerable<T> supports a single type parameter. If you were to look at Dictionary<TKey, TValue> you would notice an arity value of 2. 回答2: Within the .NET type system, it is necessary that types have unique names. Although it is only possible to create instances of bound generic types (e.g.

LINQ Getting Unique items from a List within a List

六月ゝ 毕业季﹏ 提交于 2019-12-23 07:50:55
问题 I have a List of type MyObject with a definition below: class MyObject { public string ListName { get; set; } public IEnumerable<OtherObject> ObjectList { get; set;} } Given a List of type MyObject , using LINQ what expression should I use to get all distinct OtherObject ? What I was planning to do was loop each MyObject and get the distinct OtherObject from the ObjectList property, but then I need to get the distinct across the list. Please note that if: MyObject[0].Objectlist[0] == 'ItemA'

Using Lambda Expressions trees with IEnumerable

走远了吗. 提交于 2019-12-23 07:38:11
问题 I've been trying to learn more about using Lamba expression trees and so I created a simple example. Here is the code, this works in LINQPad if pasted in as a C# program. void Main() { IEnumerable<User> list = GetUsers().Where(NameContains("a")); list.Dump("Users"); } // Methods public IEnumerable<User> GetUsers() { yield return new User{Name = "andrew"}; yield return new User{Name = "rob"}; yield return new User{Name = "chris"}; yield return new User{Name = "ryan"}; } public Expression<Func

Should an IEnumerable iterator on a Queue dequeue an item

≯℡__Kan透↙ 提交于 2019-12-23 06:57:14
问题 I have created a custom generic queue which implements a generic IQueue interface, which uses the generic Queue from the System.Collections.Generic namespace as a private inner queue. Example has been cleaned of irrelevant code. public interface IQueue<TQueueItem> { void Enqueue(TQueueItem queueItem); TQueueItem Dequeue(); } public class CustomQueue<TQueueItem> : IQueue<TQueueItem> { private readonly Queue<TQueueItem> queue = new Queue<TQueueItem>(); ... public void Enqueue(TQueueItem

Run a method on all objects within a collection

末鹿安然 提交于 2019-12-23 04:54:47
问题 So I have a collection of Razzies created from a Collection of Bloops. I retrieve this collection using a Linq query. Reference:Linq Select Certain Properties Into Another Object? for the query. I would like to know if it is possible to run a method on all of the newly created Razzies before returning the collection, or even right after, just without using a for-loop. I tried this: Dim results = From item In bloops _ Select New Razzie() With _ { _ .FirstName = item.FirstName, _ .LastName =