ienumerable

IEnumerable model property in an ASP.NET MVC 3 Editor Template

拜拜、爱过 提交于 2020-01-01 05:52:07
问题 I have a model which has an IEnumerable property (warning pseudo-code to follow) public class PersonModel { public string Name { get; set; } public IEnumerable<AddressModel> Addresses { get; set; } } public class AddressModel { public string Name { get; set; } } I want to display the Address sub-objects in the same view Person.cshtml @model PersonModel <form> <h2>Person</h2> @Html.EditorFor(m=>m.Name) <ul>@Html.EditorFor(m=>m.Addresses)</ul> </form> EditorTemplate/AddressModel @model

nest yields to return IEnumerable<IEnumerable<T>> with lazy evaluation

好久不见. 提交于 2020-01-01 04:17:12
问题 I wrote a LINQ extension method SplitBetween analogous to String.Split . > new List<int>(){3,4,2,21,3,2,17,16,1} > .SplitBetween(x=>x>=10) [3,4,2], [3,2], [], [1] Source: // partition sequence into sequence of contiguous subsequences // behaves like String.Split public static IEnumerable<IEnumerable<T>> SplitBetween<T>(this IEnumerable<T> source, Func<T, bool> separatorSelector, bool includeSeparator = false) { var l = new List<T>(); foreach (var x in source) { if (separatorSelector(x)) { if

Is there a lazy `String.Split` in C#

不打扰是莪最后的温柔 提交于 2020-01-01 04:12:08
问题 All string.Split methods seems to return an array of strings ( string[] ). I'm wondering if there is a lazy variant that returns an IEnumerable<string> such that one for large strings (or an infinite length IEnumerable<char> ), when one is only interested in a first subsequences, one saves computational effort as well as memory. It could also be useful if the string is constructed by a device/program (network, terminal, pipes) and the entire strings is thus not necessary immediately fully

How to convert type int[] to int?[]

点点圈 提交于 2019-12-31 04:34:25
问题 I'm using a linq query to output an int array. But I need to pass this into a method that only accepts int?[]. So after searching on ways to convert int[] to int?[] I found something that seemed might work here The following code is a simplified example that shows what is working and not working. using System; using System.Collections.Generic; using System.Web; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { // working... int[] vids1 = new[] { "",

How is the iteration variable readonly?

北慕城南 提交于 2019-12-31 01:54:12
问题 In 8.8.4 of the C# specification, it provides this example: A foreach statement of the form foreach (V v in x) embedded-statement is then expanded to: { E e = ((C)(x)).GetEnumerator(); try { V v; while (e.MoveNext()) { v = (V)(T)e.Current; embedded-statement } } finally { … // Dispose e } } It also says: The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement. The variable v is read-only in the embedded statement. How is the

What is the proper pattern for handling Enumerable objects with a yield return?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 18:57:11
问题 Does there exist a standard pattern for yield returning all the items within an Enumerable? More often than I like I find some of my code reflecting the following pattern: public IEnumerable<object> YieldReturningFunction() { ... [logic and various standard yield return] ... foreach(object obj in methodReturningEnumerable(x,y,z)) { yield return obj; } } The explicit usage of a foreach loop solely to return the results of an Enumerable reeks of code smell to me. Obviously I could abandon the

IEnumerable<string> to Stream for FileStreamResult

狂风中的少年 提交于 2019-12-30 10:44:24
问题 I have an IEnumerable<string> , which is "streamed" per yield statements from a method. Now I want to convert this enumerable to a Stream to use it as streamed result. Any ideas how I can do this? What I finally want to do is to return the Stream as FileStreamResult from an ASP.NET controller action. This result should be streamed as download to the client. What I do NOT want to do is to write the whole content of the IEnumerable to the stream before I return the result. This would eliminate

Is an infinite enumerable still “enumerable”?

别来无恙 提交于 2019-12-30 10:11:45
问题 Like two overlapping line segments, we can find infinite points of intersection. To enumerate all these points might not make sense, and we might just want to present that this collection is infinity. Floating point numbers have defined NegativeInfinity and PositiveInfinity . A number which represents count or ordinal seem not necessary to use floating point numbers, however, integers are not defined something to represent infinity. So I tried to implement an infinite enumerable. But I

Invalid cast from List<MyType> to IEnumerable<MyType> back to List<MyType>, why?

核能气质少年 提交于 2019-12-30 09:39:10
问题 So basically i have this method. public List<Customer> FilterCustomersByStatus(List<Customer> source, string status) { return (List<Customer>)source.Where(c => c.Status == status); } I throws me an error that it cannot cast: Unable to cast object of type 'WhereListIterator`1[AppDataAcces.Customer]' to type 'System.Collections.Generic.List`1[AppDataAcces.Customer]'. Why...? since the underlying type is the same, does the Enumerable.Where create a new instance of WhereListIterator and if so why

Invalid cast from List<MyType> to IEnumerable<MyType> back to List<MyType>, why?

廉价感情. 提交于 2019-12-30 09:39:02
问题 So basically i have this method. public List<Customer> FilterCustomersByStatus(List<Customer> source, string status) { return (List<Customer>)source.Where(c => c.Status == status); } I throws me an error that it cannot cast: Unable to cast object of type 'WhereListIterator`1[AppDataAcces.Customer]' to type 'System.Collections.Generic.List`1[AppDataAcces.Customer]'. Why...? since the underlying type is the same, does the Enumerable.Where create a new instance of WhereListIterator and if so why