ienumerable

Performance comparison of IEnumerable and raising event for each item in source?

馋奶兔 提交于 2019-12-19 04:12:44
问题 I want to read big binary file containing millions of records and I want to get some reports for the records. I use BinaryReader to read (which I think has the best performance in readers) and convert read bytes to data model. Due to the count of records, passing model to the report layer is another issue: I prefer to use IEnumerable to have LINQ functionality and features when developing the reports. Here is sample data class: Public Class MyData Public A1 As UInt64 Public A2 As UInt64

C# Class is IEnumerable AND an IEnumerator at the same time. What are the issues with this?

牧云@^-^@ 提交于 2019-12-19 02:07:08
问题 I have a class called GenericPermutations that is both enumerable and an enumerator. Its job is to take an ordered list of objects and iterate through each permutation of them in order. Example, an integer implemenation of this class could iterate through the following: GenericPermutations<int> p = new GenericPermutations<int>({ 1, 2, 3 }); p.nextPermutation(); // 123 p.nextPermutation(); // 132 p.nextPermutation(); // 213 // etc. So its enumerable in the sense that it contains a 'list' of

With Entity Framework is it better to use .First() or .Take(1) for “TOP 1”?

主宰稳场 提交于 2019-12-18 18:35:52
问题 We are implementing some EF data repositories, and we have some queries which would include TOP 1 I have read many posts suggesting to use .Take(1) The code I'm reviewing uses .First() I understand that both of these produce the same result for the object assignment, but do they both actually resolve to the same query? When the DB is queried, will it actually be with TOP 1 for both requests? Or will they execute the query in full into the enumerable, then simply take the first entry in the

c#4.0: int a real subtype of object? covariance, ienumerable and value types

て烟熏妆下的殇ゞ 提交于 2019-12-18 15:35:11
问题 I wonder why IEnumerable<int> can't be assigned to a IEnumerable<object> . After all IEnumerable is one of the few interfaces that supports covariance... The subtype relation and covariance stuff works with reference types int seems to be a proper subtype of object The combination of both features doesn't work however... class A { } class B : A { } class Program { static void Main(string[] args) { bool b; b = typeof(IEnumerable<A>).IsAssignableFrom(typeof(List<B>)); Console.WriteLine(

return single instance object as IEnumerable

时光怂恿深爱的人放手 提交于 2019-12-18 14:48:28
问题 I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc.. Perhaps something like the following: IEnumerable<foo>.fromInstance(foo) 回答1: Options: Create an instance of a collection class, like an array or a list. This would be mutable by default, which would be slightly unhelpful if this is a sequence you want to be able to hand out in your API. You could create a ReadOnlyCollection<T> wrapper around such a collection though. Write

How to combine two types of C# lists into one?

懵懂的女人 提交于 2019-12-18 14:41:38
问题 I have created two lists say X & Y. These two lists are of different types. (ie List<class_A> X & List<class_B> Y ). Values in both these lists are different. But there is a DateTime field in both these lists. I need to sort these lists according to the date field. I have separate functions to print the details of list A and List B. Suppose the sorted list looks like tuple from list A, tuple from list B, tuple from list A, tuple from list B, My purpose is to loop through this list and call

C#中的IEnumerable接口深入研究

人盡茶涼 提交于 2019-12-18 12:41:38
C#和VB.NET中的LINQ提供了一种与SQL查询类似的“对象查询”语言,对于熟悉SQL语言的人来说除了可以提供类似关联、分组查询的功能外,还能获取编译时检查和Intellisense的支持,使用Entity Framework更是能够自动为对象实体的查询生成SQL语句,所以很受大中型信息系统设计者的青睐。 IEnumerable这个接口可以说是为了这个特性“量身定制”,再加上微软提供的扩展(Extension)方法和Lambda表达式,给开发者带来了无穷的便利。本人在最近的开发工作中使用了大量的这种特性,同时在调试过程中还遇到了一个小问题,那么正好趁此机会好好研究一下相关原理和实现。 先从一个现实的例子开始吧。假如我们要做一个商品检索功能(这只是一个例子,我当然不可能把公司的产品业务也在这里贴出来),其中有一个检索条件是可以指定厂家的名称并进行模糊匹配。厂家的包括两个名称:注册名称和一般性名称,我们只按一般性名称进行检索。当然你可以说直接用SQL查询就行了,但是我们的系统是以实体对象为核心进行设计的,厂家的数量也不会太多,大概1000条。为了不增加系统的复杂性,只考虑使用现有的数据访问层接口进行实现(按过滤条件获取商品,以及获取所有厂商),这时LINQ的便捷性就体现出来了。 借助IEnumerable接口和其辅助类,我们可以写出以下代码: public

Best method to use IDataReader as IEnumerable<T>?

佐手、 提交于 2019-12-18 12:39:03
问题 I need to use Linq on any IDataReader implementations like this var c = sqlDataReader.AsEnumerable().Count(); Example: public abstract class Test { public abstract SqlDataReader GetSqlDataReader(); public void Foo() { SqlDataReader sqlDataReader = GetSqlDataReader(); IEnumerable<SqlDataReader> sqlEnumerable = sqlDataReader.AsEnumerable(); var c = sqlEnumerable.Count(); var s = sqlEnumerable.Sum(); SqlDataReader first = sqlEnumerable.First(); var t = first.GetSqlXml(10); } } What is the best

Question regarding IEnumerable and IEnumerator

冷暖自知 提交于 2019-12-18 12:32:53
问题 I use the following code to enable myClass to use foreach. But I am rather new to programming and have some difficulty in understanding the following code. I described my problems in the comments. I would be grateful for providing some information. public class MyClass : IEnumerable<string> { //1) What is IEnumerator for? // Whats the difference between IEnumerator and IEnumerable public IEnumerator<string> GetEnumerator() { yield return "first"; yield return "second"; } //2) What is it for?

Caching IEnumerable

ぃ、小莉子 提交于 2019-12-18 11:21:18
问题 public IEnumerable<ModuleData> ListModules() { foreach (XElement m in Source.Descendants("Module")) { yield return new ModuleData(m.Element("ModuleID").Value); } } Initially the above code is great since there is no need to evaluate the entire collection if it is not needed. However, once all the Modules have been enumerated once, it becomes more expensive to repeatedly query the XDocument when there is no change. So, as a performance improvement: public IEnumerable<ModuleData> ListModules()