ienumerable

IEnumerable和IEnumerator 详解

十年热恋 提交于 2019-12-26 05:14:28
http://blog.csdn.net/byondocean/article/details/6871881 初学C#的时候,老是被IEnumerable、IEnumerator、ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质。 下面我们先看IEnumerable和IEnumerator两个接口的语法定义。其实IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。那么让我们看看IEnumerator接口有定义了什么东西。看下图我们知道IEnumerator接口定义了一个Current属性,MoveNext和Reset两个方法,这是多么的简约。既然IEnumerator对象时一个访问器,那至少应该有一个Current属性,来获取当前集合中的项吧。 MoveNext方法只是将游标的内部位置向前移动(就是移到一下个元素而已),要想进行循环遍历,不向前移动一下怎么行呢? 详细讲解:

Primary key set to zero on submit of IEnumerable<T>

做~自己de王妃 提交于 2019-12-25 08:18:23
问题 So I created a variable length list using the code shown here. When I submit the form, the primary keys are reset to 0. In the HTML form, they are not zero but the actual values. How do I fix this? The form @using (Html.BeginCollectionItem("Kortingartikel")) { @Html.HiddenFor(x => x.Artikelid) @Html.TextBoxFor(x => x.Artikelnaam) @Html.TextBoxFor(x => x.Prijs)</td> } The data According to Chrome, this data is sent to the server: Kortingartikel.index:ad56efb0-ab7f-4b37-9d9b-712d7c3e3543

IEnumerable - implementing using GetEnumerator from local variable

匆匆过客 提交于 2019-12-25 03:11:09
问题 I'm newb with generics/iterators/enumerators etc. I have code, it keeps field number (int) and error mesages (List string) for each field: public class ErrorList : IEnumerable // ? { private Dictionary <int, List<string>> errorList; // ... } How to make this class work with foreach loop? I wanna use GetEnumerator form Dictionary, but how should i do this? 回答1: You could simply provide a public GetEnumerator method: public class ErrorList { private Dictionary<int, List<string>> errorList = new

ICollection acting as IEnumerable

半世苍凉 提交于 2019-12-24 20:08:35
问题 I am accessing the Keys of an OrderedDictionary : Why does it say this?: Expanding the Results View will enumerate the IEnumerable I thought anything more concrete than an IEnumerable (eg ICollection , IList etc) are already enumerated? 回答1: List<T> implements both IList<T> and IEnumerable<T> . Logically, it cannot be both already enumerated and not already enumerated. The interfaces only define a contract through which you can interact with the object. What matters is the underlying type

How do you create an enumeration of random values?

青春壹個敷衍的年華 提交于 2019-12-24 19:19:49
问题 How can I get an IEnumerable of random values? I'm disappointed the class Random doesn't implement IEnumerable<int> . 回答1: Write an extension method that will yield return new values. public static IEnumerable<int> GetRandomValues(this Random instance) { while(true) { yield return instance.Next(); } } 回答2: You can implement this yourself fairly easily, for example public IEnumerable<int> RandomInts(int below) { Random r = new Random(); while (true) yield return r.Next(below); } 回答3: The some

How to convert an IEnumerable into a string to put it in a message box in c#? [duplicate]

倖福魔咒の 提交于 2019-12-24 18:46:06
问题 This question already has answers here : How to convert IEnumerable<string> to one comma separated string? (6 answers) Closed 5 months ago . I have an IEnumerable which contains differences of two files. I want to show these differences in a message box. I tried to convert it with the toArray() method and with the toString() method. IEnumerable<String> inFirstNotInSecond = file1Lines.Except(file2Lines); IEnumerable<String> inSecondNotInFirst = file2Lines.Except(file1Lines); //i also tried

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

夙愿已清 提交于 2019-12-24 16:56:08
问题 Can you please help with the error below? What is wrong with my codes. Thanks for your help. Error: The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable'. controller public ActionResult ProductCreate() { ViewBag.Suppliers = db.Suppliers.Where(x => x.IsActive).ToList(); ViewBag.Categories = new SelectList(db.Categories.Where(x => x.IsActive).ToList(), "Id", "Name").ToList(); return View(); } [HttpPost] [ValidateAntiForgeryToken] public

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

白昼怎懂夜的黑 提交于 2019-12-24 16:55:03
问题 Can you please help with the error below? What is wrong with my codes. Thanks for your help. Error: The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable'. controller public ActionResult ProductCreate() { ViewBag.Suppliers = db.Suppliers.Where(x => x.IsActive).ToList(); ViewBag.Categories = new SelectList(db.Categories.Where(x => x.IsActive).ToList(), "Id", "Name").ToList(); return View(); } [HttpPost] [ValidateAntiForgeryToken] public

String Split by Length and split only by nearest space

杀马特。学长 韩版系。学妹 提交于 2019-12-24 14:27:37
问题 I am having a Text Like var data = "âô¢¬ôè÷¢ : ªîø¢è¤ô¢ - ã¿ñ¬ô ñèù¢ ªð¼ñ£÷¢ ï¤ôñ¢,«ñø¢è¤ô¢ - ªð¼ñ£÷¢ ñèù¢ ÝÁºèñ¢ ï¤ô袰ñ¢ ñ¤ì¢ì£ Üò¢òñ¢ ªð¼ñ£ñ¢ð좮 è¤ó£ñ âô¢¬ô袰ñ¢,õìè¢è¤ô¢ - ÝÁºèñ¢ ï¤ôñ¢,è¤öè¢è¤ô¢ - ô좲ñ¤ ï¤ôñ¢ ñø¢Áñ¢ 1,22 ªê ï¤ôñ¢ ð£î¢î¤òñ¢"; and I am Having the Extension Method to split string public static IEnumerable<string> EnumByLength(this string s, int length) { for (int i = 0; i < s.Length; i += length) { if (i + length <= s.Length) { yield return s.Substring(i, length); }

How to cast list to enumerable

那年仲夏 提交于 2019-12-24 07:48:15
问题 I've got a problem with the following code: public IEnumerable<ISession> GetSessions() { // ... using (ProvaDbEntities DBEntities = new ProvaDbEntities(Utilities.ToEntitiesConnectionString())) { ObjectQuery<session> sessions = DBEntities.session; IEnumerable<session> q1 = from session in sessions where session.site == this.Name select session; List<Session> sessionList = new List<Session>(); foreach (var s in q1) { sessionList.Add(new Session(s.id.ToString(),s.username, s.site, new DateTime()