ienumerable

C#: Return IEnumerable when field is not null?

最后都变了- 提交于 2019-12-23 03:57:26
问题 public IEnumerable GetAddress() { DataSet ds = DataOps.GetDataSet(string.Format(" select * from Students")); DataTable dt = ds.Tables[0]; // What goes here? } I need to use IEnumerable methods How can i return enumeration of DataRows containing all students that have addresses only? 回答1: I think what you are looking is DataRow[] dr = ds.Tables[0].Select("Address NOT NULL"); // you want filtering on address column foreach (DataRow row in dr) { } 回答2: I don't know what your student class looks

XmlSerializer doesn't serialize everything in my class

旧巷老猫 提交于 2019-12-23 02:17:54
问题 I have a very basic class that is a list of sub-classes, plus some summary data. [Serializable] public class ProductCollection : List<Product> { public bool flag { get; set; } public double A { get; set; } public double B { get; set; } public double C { get; set; } } ... // method to save this class private void SaveProductCollection() { // Export case as XML... XmlSerializer xml = new XmlSerializer(typeof(ProductCollection)); StreamWriter sw = new StreamWriter("output.xml"); xml.Serialize(sw

Where does a generic List<> implement Reset?

一个人想着一个人 提交于 2019-12-22 12:14:49
问题 when I go to the definition of List<> I can see it has a public struct Enumerator that implements the interfaces IEnumerator<T> , IDisposable and IEnumerator. IEnumerator should force the implementation of Reset - besides Current and MoveNext. Yet only Current and MoveNext are implemented. How can that be? Where do I find the Reset() of List<>? var list = new List<int>(); list.Add(23); list.Add(44); var Enumerator = list.GetEnumerator(); while (Enumerator.MoveNext()) { Console.WriteLine

How do you transfer the execution of a Expression created by an IQueryable object to a IEnumerable?

喜欢而已 提交于 2019-12-22 11:01:54
问题 In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree. Now if I want to mock my repository in theorie this is very easy : just implement the interface of my repository (which is also a IQueryable object). My mock repository implementation would be only a in memory collection, but my question is : Do you know an easy way to implement the IQueryable interface of my mock, to transfer the query to my in-memory collection

Move item up in IEnumerable

て烟熏妆下的殇ゞ 提交于 2019-12-22 10:07:19
问题 I have a need to move an item in an IEnumerable<> up, that is move one item above another. What is the simplest way to do this? A similar question was asked here but I don't have a generic list only an IEnumerable<>: Generic List - moving an item within the list 回答1: As @Brian commented the question is a little unclear as to what move an item in an IEnumerable<> up means. If you want to reorder an IEnumerable for a single item then the code below should might be what you are looking for.

Linq - Dynamic GroupBy with IEnumerable<T>

醉酒当歌 提交于 2019-12-22 08:41:44
问题 I have a collection that is IEnumerable<Transaction> . Transaction has several properties such as TransactionId (Int64), PaymentMethod(string) and TransactionDate(DateTime) I'd like to be able to accomplish this transactions.GroupBy(x => x.PaymentMethod) dynamically at run time based on whatever grouping field the user has decided to use. I found most of the answer I'm looking for in dtb's answer here Linq GroupBy - how to specify the grouping key at runtime? This works well: var arg =

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

浪子不回头ぞ 提交于 2019-12-22 08:33:54
问题 I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it). Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used? 回答1: The int parameter

Recommended behaviour of GetEnumerator() when implementing IEnumerable<T> and IEnumerator<T>

时光毁灭记忆、已成空白 提交于 2019-12-22 05:04:16
问题 I am implementing my own enumerable type. Something ressembling this: public class LineReaderEnumerable : IEnumerable<string>, IDisposable { private readonly LineEnumerator enumerator; public LineReaderEnumerable(FileStream fileStream) { enumerator = new LineEnumerator(new StreamReader(fileStream, Encoding.Default)); } public IEnumerator<string> GetEnumerator() { return enumerator; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { enumerator.Dispose

Recommended behaviour of GetEnumerator() when implementing IEnumerable<T> and IEnumerator<T>

寵の児 提交于 2019-12-22 05:04:10
问题 I am implementing my own enumerable type. Something ressembling this: public class LineReaderEnumerable : IEnumerable<string>, IDisposable { private readonly LineEnumerator enumerator; public LineReaderEnumerable(FileStream fileStream) { enumerator = new LineEnumerator(new StreamReader(fileStream, Encoding.Default)); } public IEnumerator<string> GetEnumerator() { return enumerator; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { enumerator.Dispose

IEnumerable<T> vs IReadOnlyList<T>

让人想犯罪 __ 提交于 2019-12-22 04:31:35
问题 What is the difference between choosing IEnumerable<T> vs IReadOnlyList<T> as a return parameter type or input parameter type? IEnumerable<T> provides .Count and .ElementAt which is what is exposed by IReadOnlyList<T> 回答1: IEnumerable<T> represents a forward-only cursor over some data. You can go from start to end of the collection, looking at one item at a time. IReadOnlyList<T> represents a readable random access collection. IEnumerable<T> is more general, in that it can represent items