ienumerable

What Should I use? a dictionary, list, or something else [closed]

痞子三分冷 提交于 2020-02-23 08:27:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed yesterday . Please excuse the very entry-level nature of this question. I'm very new to c#. When I work with various libraries and components that have objects stored in collections, I'm able to retrieve the object by either the object name or its index. I want to implement a similar collection in c#; however, my

LINQ - For each item in select populate value to one unpopulated property

主宰稳场 提交于 2020-02-08 10:46:31
问题 I'm using my Map method to create DTO object from my context class Company and Mapping looks like this: private CompDTO Map(Company company) { return new CompDTO() { Id = company.Id, Title = company.Title, ParentCompanyId = company.ParentCompanyId, }; } CompDTO looks like this: public class CompDTO { public long Id { get; set; } public string Title { get; set; } public long? ParentCompanyId { get; set; } public bool HasChildrens { get; set; } } Here's how I'm calling my Map method : private

IList,ICollection,IEnumerable,IEnumerator,IQueryable

会有一股神秘感。 提交于 2020-02-04 07:30:21
IList 是 ICollection 接口的子代,并且是所有非泛型列表的基接口。IList 实现有三种类别:只读、固定大小和可变大小。无法修改只读 IList。固定大小的 IList 不允许添加或移除元素,但允许修改现有元素。可变大小的 IList 允许添加、移除和修改元素。 ICollection 接口是 System.Collections 命名空间中类的基接口。ICollection 接口扩展 IEnumerable;IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。 IDictionary 实现是键/值对的集合,如 Hashtable 类。 IList 实现是值的集合,其成员可通过索引访问,如 ArrayList 类。 某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现 ICollection 接口。 如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。定义所有非泛型集合的大小、枚举器和同步方法。 IQueryable 提供对未指定数据类型的特定数据源的查询进行计算的功能,IQueryable 接口由查询提供程序实现。 该接口只能由同时实现 IQueryable(Of T) 的提供程序实现。 如果该提供程序不实现

Cannot implicity convert type System.Collections.Generic.IEnumerable<B> to System.Collections.Generic.List<B>

[亡魂溺海] 提交于 2020-02-03 13:24:46
问题 With the code below i get this error and need help with how to let method Load return List<B> Cannot implicity convert type System.Collections.Generic.IEnumerable to System.Collections.Generic.List public class A { public List<B> Load(Collection coll) { List<B> list = from x in coll select new B {Prop1 = x.title, Prop2 = x.dept}; return list; } } public class B { public string Prop1 {get;set;} public string Prop2 {get;set;} } 回答1: Your query returns an IEnumerable , while your method must

DataTable iteration gives unwanted data

折月煮酒 提交于 2020-01-22 02:31:13
问题 I am iterating through a DataTable using SqlDataAdapter and storing it in a list. Here is my code. public IEnumerable<DataRow> GetRecord() { var table = new DataTable(); using (var da = new SqlDataAdapter("SELECT * FROM mv_tbl", "ConnectionString")) { da.Fill(table); List<DataRow> list = new List<DataRow>(); foreach (DataRow r in table.Rows) { list.Add(r); } return list; } } This gives me a result which has unnecessary data also. Here is the result. [{"RowError":"", "RowState":2, "Table":[{

Correct way to check if IEnumerable<T> is created by a yield keyword

。_饼干妹妹 提交于 2020-01-21 07:36:10
问题 What is the correct way to check if an IEnumerable<T> is generated by the yield keyword ? Sample : public IEnumerable<int> GetMeSomeInts() { // Unknown implementation } Somewhere : IEnumerable<int> someInts = GetMeSomeInts() ; if (someInts is generatedbyayield) // <- What should be this condition ? someInts = someInts.ToList() ; 回答1: The state machine created by the yield keyword was not designed to be "detectable". If you find a way to detect it, you will have to rely on some implementation

Linq To Sql return from function as IQueryable<T>

廉价感情. 提交于 2020-01-21 07:35:06
问题 Ok, I have managed to get the following working public IQueryable getTicketInformation(int ticketID) { var ticketDetails = from tickets in _context.tickets join file in _context.file_objects on tickets.ticket_id equals file.source_id where tickets.ticket_id == ticketID select new { tickets.ticket_id, tickets.title, tickets.care_of_email, file.filename }; return ticketDetails.AsQueryable(); } I went ahead and created my own class (myObject) containing the primitives ticket_id, title, care_of

What is the difference between the non-generic IEnumerable and the generic IEnumerable<T>?

匆匆过客 提交于 2020-01-21 01:46:06
问题 Sorry for such a vague question, but I have been searching around for the best part of a day, I have read article after article (and many questions here) but just cannot find an easy to understand answer. I (think I) know what IEnumerable is for, but I just can't understand what it means when it is defined with a generic type argument, for example: IEnumerable<int> test = method(); This is just driving me mad! Please put me out of misery and explain what it means? 回答1: An IEnumerable is

C#数组和集合类详解

感情迁移 提交于 2020-01-20 18:34:48
一.数组 变量是程序运行时在内存中存储发生变化的容器。 变量可以存储单个数据,而数组可以存储多个变量,并且互不干扰,互不影响,和谐共处。 数组的特点:长度固定,类型相同。 二.数组的分类 一维数组,二维数组,多维数组,不规则数组 三.数组的应用 创建: int[] vs = new int[5]; 赋值: vs[0] = 5; 应用 四.声明数组 //定义一个已知长度的一维数组 int [ ] arr = new int [ 100 ] ; //定义一个未知长度并使用对象初始化器直接赋值int string person_Names = new string [ ] { "张三" , "李四" , "王五" , "刘涛" } ; 五.C#多维数组 C#支持多维数组,多维数组又称为矩阵数组。 声明一个string变量的多维数组 string [ , ] names 声明一个int类型的多维数组: int[,,] m 二维数组 :多维数组最简单的形式就是二维数组。一个二维数组在本质上可以被认为是一个带有X,Y的表格。 int[,] a=new int[3,4]{{0,1,2,4}{5,6,7,8}}; 可以通过唯一索引(即行索引和列索引来访问数据) 使用循环来处理二维数组: //定义一个多维数组,二行四列 int [ , ] vs = new int [ 2 , 4 ] { { 0 ,

集合的枚举和排序

送分小仙女□ 提交于 2020-01-19 08:49:06
原文 实现了IEnumerable接口才能遍历 先来看看IEnumerable接口的定义: public interface IEnumerable { IEnumerator GetEnumerator(); } 这个接口非常简单,主要就是一个方法GetEnumerator,用来返回一个IEnumerator对象。继续深入下去,IEnumerator接口的定义如下: public interface IEnumerator { bool MoveNext(); void Reset(); object Current{get;} } 上面的IEnumerator接口定义的属性和方法,只有一个目的,就是实现如何遍历。下面具体解释一下: Current属性:用来返回当前集合项的值 MoveNext方法:移动到集合对象的下一项,如果有下一项,就返回true。如果没有,就返回false。 Reset方法:重置然后可以重头访问集合 当用foreach循环来访问一个对象的时候,其实是默认的调用了对象里面的GetEnumerator()方法。而该方法就是由IEumerable接口给暴露出来的,所以如果想自定义一个能实现枚举功能的集合类就要先实现一个IEnumerable接口,然后实现里面的GetEnumerator方法。如下所示: namespace Test { class Program