ienumerable

understanding IEnumerable<T> in C#

僤鯓⒐⒋嵵緔 提交于 2020-01-11 06:55:38
问题 I am trying to understand the IEnumerable interface in C#. As it is stated in MSDN IEnumerable exposes an enumerator, which supports a simple iteration over a non-generic collection. This is quite simple. When we have a collection that implements this interface then, we get an enumerator, which implements the IEnumerator interface, (http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator(v=vs.110).aspx), and the using this enumerator we can iterate through all the

Is IEnumerable required to use a foreach loop? [duplicate]

筅森魡賤 提交于 2020-01-11 04:23:08
问题 This question already has answers here : Does Class need to implement IEnumerable to use Foreach (11 answers) Closed 6 years ago . I was wondering, when exactly can I use the foreach loop? Do I have to implement IEnumerable? 回答1: There is no need to implement the IEnumerable interface to use the foreach statement. Here is a quote from the MSDN (http://msdn.microsoft.com/en-us/library/9yb8xew9.aspx): In C#, it is not absolutely necessary for a collection class to inherit from IEnumerable and

How to group items by index? C# LINQ

吃可爱长大的小学妹 提交于 2020-01-10 12:05:13
问题 Suppose I have var input = new int[] { 0, 1, 2, 3, 4, 5 }; How do I get them grouped into pairs? var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } }; Preferably using LINQ 回答1: input .Select((value, index) => new { PairNum = index / 2, value }) .GroupBy(pair => pair.PairNum) .Select(grp => grp.Select(g => g.value).ToArray()) .ToArray() 回答2: var input = new int[] { 0, 1, 2, 3, 4, 5 }; int i = 0; var output = input.GroupBy(item => i++ / 2); 回答3: Probably not

Pass IEnumerable list to controller

纵饮孤独 提交于 2020-01-10 05:31:27
问题 In my asp.net MVC 4 project, I'm trying to pass IEnumerable list from view to controller. The problem is that the list receiverd in the action is null. Any help please. This is part of my code view : @model IEnumerable<PFEApplication.Models.agent> <div id="Leader"> @using (Ajax.BeginForm("AddNewLeader", "Equipe", FormMethod.Post, new AjaxOptions { UpdateTargetId = "Leader", HttpMethod = "Post", InsertionMode = InsertionMode.Replace })) { foreach (var item in Model) { @Html.HiddenFor(modelItem

What is it called when you edit an interface?

两盒软妹~` 提交于 2020-01-10 05:22:25
问题 I am going through a LitJSON library. In the code there are lots of segments like public class JsonData : IJsonWrapper, IEquatable<JsonData> #region ICollection Properties int ICollection.Count { get { return Count; } } #end region For a method I know how overriding/overloading works but in the example above the code reads: int ICollection.Count I'm not familiar with that format for a method signature. Is the coder trying to explicitly state its the ICollection.Count interface? Could you

Why use .AsEnumerable() rather than casting to IEnumerable<T>?

喜欢而已 提交于 2020-01-09 06:05:10
问题 One of the extension methods on IEnumerable<T> is .AsEnumerable() . This method converts the enumerable object it was called on into an instance of IEnumerable<T> . However, since an object must implement IEnumerable<T> in order to apply to this extension method, converting to IEnumerable<T> is a simple matter of casting to IEnumerable<T> . My question is why does this method exist at all? Example: List<string> strings = new List<string>() { "test", "test2", "test3" }; IEnumerable<string>

An example for supporting foreach without implementing IEnumerable

℡╲_俬逩灬. 提交于 2020-01-08 04:00:15
问题 I am looking at this blog which explains that foreach can be supported without implementing IEnumerable . But does not go into the details of the implementation. I am looking for an example of how to support foreach without implementing IEnumerable . Edit: Thanks to @Sam I am 's comment, I got what I was looking for. (see my answer below) 回答1: Here is a class that doesn't implement IEnumerable , or any interfaces at all: public class Foo { public IEnumerator<int> GetEnumerator() { yield

An example for supporting foreach without implementing IEnumerable

半世苍凉 提交于 2020-01-08 03:59:53
问题 I am looking at this blog which explains that foreach can be supported without implementing IEnumerable . But does not go into the details of the implementation. I am looking for an example of how to support foreach without implementing IEnumerable . Edit: Thanks to @Sam I am 's comment, I got what I was looking for. (see my answer below) 回答1: Here is a class that doesn't implement IEnumerable , or any interfaces at all: public class Foo { public IEnumerator<int> GetEnumerator() { yield

Replace NULL with a default Value in an ItemsSource IEnumerable

最后都变了- 提交于 2020-01-07 12:02:06
问题 I'm having a Custom Control derived from an ItemsControl . I got the idea from Two-Way Binding Issue of Unknown Object in WPF Custom Control Dependency Property In the above Question, they use the Collection in the View Model private ObservableCollection<string> _collection = new ObservableCollection<string>(); public ObservableCollection<string> Collection { get { return _collection; } set { _collection = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(

How can I return a list of rows of dynamic table? [duplicate]

℡╲_俬逩灬. 提交于 2020-01-07 08:36:12
问题 This question already has answers here : DataTable iteration gives unwanted data (2 answers) Anonymous type result from sql query execution entity framework (5 answers) Closed 5 days ago . My previous question was closed due to typo. But my issue is unresolved. So rephrasing the question. Hi, I have a query which returns data dynamically. Number of columns, their name and datatypes are not known. It's like a Select * from table_name I need to get the result in a list. I tried with IEnumerable