ienumerable

Using LINQ GroupBy to group by reference objects instead of value objects

匆匆过客 提交于 2020-08-26 09:56:13
问题 I want to GroupBy multiple objects in a list of records and not simply multiple values. I am having trouble getting grouping to work with reference type objects. I have a collection of objects that contain Room, Type, and DateTime. Room, Type, and DateTime all have properties associated with them. I've already added the IEquateable interface to the room and the type thinking that would be enough to with with group by. var groups = collection .Where(g => g.Stage == InventoryStage.StageA)

C#: SkipLast implementation

非 Y 不嫁゛ 提交于 2020-08-24 03:44:10
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

C#: SkipLast implementation

我的未来我决定 提交于 2020-08-24 03:43:27
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

C#: SkipLast implementation

≡放荡痞女 提交于 2020-08-24 03:43:07
问题 I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { var current = iterator.Current; if(!iterator.MoveNext()) yield break; yield return current; } } } What I need it for is to do something with all the items except the last one. In my case I have a sequence of objects with various

Why doesn't Any() work on a c# null object

こ雲淡風輕ζ 提交于 2020-08-21 04:57:28
问题 When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren't 'any', and it should probably return false. Why does C# behave this way? 回答1: When dealing with reference types, a null value is semantically different from an "empty" value. A null string is not the same as string.Empty , and a null IEnumerable<T> is not the same as Enumerable.Empty<T> (or any other "empty" enumerable of that type). If Any were not an extension method

How to check an IEnumerable for multiple conditions with a single enumeration without buffering?

爱⌒轻易说出口 提交于 2020-07-08 20:32:30
问题 I have a very long sequence of data is the form of IEnumerable , and I would like to check it for a number of conditions. Each condition returns a value of true or false, and I want to know if all conditions are true. My problem is that I can not afford to materialize the IEnumerable by calling ToList , because it is simply too long (> 10,000,000,000 elements). Neither I can afford to enumerate the sequence multiple times, one for each condition, because each time I will get a different