ienumerable

How does Assert.AreEqual determine equality between two generic IEnumerables?

老子叫甜甜 提交于 2019-12-17 17:56:13
问题 I have a unit test to check whether a method returns the correct IEnumerable . The method builds the enumerable using yield return . The class that it is an enumerable of is below: enum TokenType { NUMBER, COMMAND, ARITHMETIC, } internal class Token { public TokenType type { get; set; } public string text { get; set; } public static bool operator == (Token lh, Token rh) { return (lh.type == rh.type) && (lh.text == rh.text); } public static bool operator != (Token lh, Token rh) { return !(lh =

Convert from List into IEnumerable format

◇◆丶佛笑我妖孽 提交于 2019-12-17 17:36:35
问题 IEnumerable<Book> _Book_IE List<Book> _Book_List How shall I do in order to convert _Book_List into IEnumerable format? 回答1: You don't need to convert it. List<T> implements the IEnumerable<T> interface so it is already an enumerable. This means that it is perfectly fine to have the following: public IEnumerable<Book> GetBooks() { List<Book> books = FetchEmFromSomewhere(); return books; } as well as: public void ProcessBooks(IEnumerable<Book> books) { // do something with those books } which

DataTables vs IEnumerable<T>

早过忘川 提交于 2019-12-17 16:55:30
问题 I'm having a debate with another programmer I work with. For a database return type, are there any significant memory usage or performance differences, or other cons which should make someone avoid using the DataSets and DataTables and favour types which implement IEnumerable<T> ... or vice versa I prefer returning types which implement IEnumerable<T> ( List<T>, T[] etc ) because it's more lightweight, strongly typed to the object when accessing properties, allows richer information about the

Convert DataRowCollection to IEnumerable<T>

泪湿孤枕 提交于 2019-12-17 16:00:12
问题 I would like to do something like this in .NET 3.5. What's the quickest way? IEnumerable<DataRow> collection = TypedDataSet.TypedTableBase<DataRow>.Rows as IEnumerable<DataRow>; 回答1: Assuming you're using .NET 4.0, which introduces covariance: // Presumably your table is of some type deriving from TypedTableBase<T>, // where T is an auto-generated type deriving from DataRow. IEnumerable<DataRow> collection = myTypedTable; The table type itself implements IEnumerable<T> where T : DataRow .

Calculating Count for IEnumerable (Non Generic)

我的未来我决定 提交于 2019-12-17 15:47:24
问题 Can anyone help me with a Count extension method for IEnumerable (non generic interface). I know it is not supported in LINQ but how to write it manually? 回答1: The simplest form would be: public static int Count(this IEnumerable source) { int c = 0; using (var e = source.GetEnumerator()) { while (e.MoveNext()) c++; } return c; } You can then improve on this by querying for ICollection : public static int Count(this IEnumerable source) { var col = source as ICollection; if (col != null) return

How can I convert a DataTable into a Dynamic object?

◇◆丶佛笑我妖孽 提交于 2019-12-17 10:19:01
问题 How can I convert a DataTable in IEnumerable<dynamicObject> ? For example, I want to convert any DataTable ID | Name DI | emaN --------- or --------- 1 | x 2 | x 2 | y 1 | y In a list of objects // list 1 (ex 1) // list 2 (ex 2) { { { ID = 1, Name = "x" } { DI = 2, emaN = "x" } { ID = 2, Name = "y" } { DI = 1, emaN = "y" } } } So list1.First().ID // 1 list2.First().emaN // "x" How can I do it? 回答1: How about with DynamicObject: public static class DataTableX { public static IEnumerable

Why was IEnumerable<T> made covariant in C# 4?

那年仲夏 提交于 2019-12-17 08:33:26
问题 In earlier versions of C# IEnumerable was defined like this: public interface IEnumerable<T> : IEnumerable Since C# 4 the definition is: public interface IEnumerable<out T> : IEnumerable Is it just to make the annoying casts in LINQ expressions go away? Won't this introduce the same problems like with string[] <: object[] (broken array variance) in C#? How was the addition of the covariance done from a compatibility point of view? Will earlier code still work on later versions of .NET or is

What is the purpose of AsQueryable()?

橙三吉。 提交于 2019-12-17 08:17:33
问题 Is the purpose of AsQueryable() just so you can pass around an IEnumerable to methods that might expect IQueryable , or is there a useful reason to represent IEnumerable as IQueryable ? For example, is it supposed to be for cases like this: IEnumerable<Order> orders = orderRepo.GetAll(); // I don't want to create another method that works on IEnumerable, // so I convert it here. CountOrders(orders.AsQueryable()); public static int CountOrders(IQueryable<Order> ordersQuery) { return

Using IEnumerable without foreach loop

二次信任 提交于 2019-12-17 07:17:16
问题 I've gotta be missing something simple here. Take the following code: public IEnumerable<int> getInt(){ for(int i = 0; i < 10; i++){ yield return i; } } I can call this with: foreach (int j in obj.getInt()){ //do something with j } How can I use the getInt method without the foreach loop: IEnumerable<int> iter = obj.getInt(); // do something with iter ?? Thanks. EDITS For those wondering why I'd want this. I'm iterating two things: IEnumerator<int> iter = obj.getInt().GetEnumerator(); foreach

Split an IEnumerable<T> into fixed-sized chunks (return an IEnumerable<IEnumerable<T>> where the inner sequences are of fixed length) [duplicate]

家住魔仙堡 提交于 2019-12-17 07:14:39
问题 This question already has answers here : Create batches in linq (16 answers) Split List into Sublists with LINQ (29 answers) Closed 2 years ago . I want to take an IEnumerable<T> and split it up into fixed-sized chunks. I have this, but it seems inelegant due to all the list creation/copying: private static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> items, int partitionSize) { List<T> partition = new List<T>(partitionSize); foreach (T item in items) { partition.Add(item); if