ienumerable

IEnumreable dynamic and lambda

♀尐吖头ヾ 提交于 2020-01-03 19:08:15
问题 I would like to use a lambda expression on a IEnumerable<dynamic> type, howerver im getting the following error on attributes and coordinates where im using a new lambda expression: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type . Here is my code public static object returnFullSelectWithCoordinates(IEnumerable<dynamic> q) { return q.Select(b => new { route_id = b.b.route_id, name = b.b.name,

How to use Directory.EnumerateFiles excluding hidden and system files

亡梦爱人 提交于 2020-01-03 09:10:10
问题 I'm enumerating all files in a directory so that I can process them later. I want to exclude hidden and system files. This is what I have so far: IEnumerable<IGrouping<string, string>> files; files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories) .Where(f => (new FileInfo(f).Attributes & FileAttributes.Hidden & FileAttributes.System) == 0) .GroupBy(Path.GetDirectoryName); However if I look at the results I am still getting hidden and system files included: foreach (var

What is the benefit of SequenceEqual?

不羁岁月 提交于 2020-01-03 05:36:28
问题 I did a comparison between SequenceEqual and element comparison in for loop. static void Main(string[] args) { //var myList = new List<short>(); //var anotherList = new List<short>(); var myList = new short[2000000]; var anotherList = new short[2000000]; for (int i = 0; i < 2000000; i++) { //myList.Add(5); //anotherList.Add(5); myList[i] = 5; anotherList[i] = 5; } var watch = System.Diagnostics.Stopwatch.StartNew(); //for (int i = 0; i < 2000000; i++) //{ // if (myList[i] != anotherList[i]) /

Fastest way to convert IEnumerable<T> to List<T> in C#

旧街凉风 提交于 2020-01-03 04:51:14
问题 In C#, what is the fastest way to create and fill a List using an IEnumberable in terms of time required to write the code for? What about in terms of time required to execute? My first thought was this: List<int> list = new List<int>(); foreach(int number in iterator) list.Add(number); Is there a faster way? 回答1: When it comes to List<T> essentially you have 2 approaches, which I am trying to discuss below. For the sake of clarity lets assume, allocation of the List<T> takes constant time (C

IQueryable vs IEnumerable: is IQueryable always better and faster?

 ̄綄美尐妖づ 提交于 2020-01-02 14:31:22
问题 If the IQueryable interface performs the query expression in the server rather than fetching all records like IEnumerable , why is IQueryable not replaced by IEnumerable where it can be faster and more efficient? DBSet<T> has two flavors of Where ( IQueryable and IEnumerable ). Is there a way to call the IEnumerable version because the IQueryable is called by default, without calling ToList() ? 回答1: If the IQueryable perform the query Expression in the server rather than fetching all records

Why does calling AsEnumerable() on a DataTable prevent a GridView from binding to it?

China☆狼群 提交于 2020-01-02 08:54:48
问题 In my .aspx page, I have an <asp:GridView runat="server" ID="CustomerGridView"> control which I bind like this: public partial class InsertCustomer : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewCustomer(); } } private void ViewCustomer() { var manager = new DomainManager(); var result = manager.FindAll(new Customer()); this.CustomerGridView.DataSource = result; this.CustomerGridView.DataBind(); } } Here is the DomainManager class: public

C# IEnumerable print out

这一生的挚爱 提交于 2020-01-02 00:55:23
问题 I am having problems with an array where I for example want to printout the odd numbers in the list. int[] numbers = new int[]{ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; Console.WriteLine(numbers.Where(n => n % 2 == 1).ToArray()); The ToString method does not seem to work? I do not want to loop through the elements. What can I do? 回答1: You need to call String.Join to create a string with the contents of the sequence. For example: Console.WriteLine(String.Join(", ", numbers.Where(n => n % 2 == 1)); This

Enumerable.Range and Memory allocation

梦想的初衷 提交于 2020-01-01 11:58:46
问题 I have the following code: IEnumerable<int> elements = Enumerable.Range(1, Int32.MaxValue); Console.WriteLine("Size of System.Int32: {0}", sizeof(int)); Console.Write("Iterating over {0} elements... ", elements.Count()); Parallel.ForEach(elements, _ => { }); Console.WriteLine("Done."); This prints out: > Size of System.Int32: 4 > Iterating over 2147483647 elements... Done. However, I don't understand why is this not throwing an OutOfMemoryException . Knowing that each int value takes up 4

How does the RemoveRange() method work in a List<>?

笑着哭i 提交于 2020-01-01 09:23:09
问题 As in title. I know it probably merges 2 sublists before and after deleted items, but how does that method behave when removing LAST elements? In other words: does it somehow make a copy of all elements located before remove index? I'm just curious about perfomance of using RemoveRange on a giant List (let's say 5000 elements) just to remove f.e. only last 2 of them. If it makes a copy then, is there a way to change some internal variable that sets size of the List (and treat rest of

IEnumerable foreach, do something different for the last element

旧时模样 提交于 2020-01-01 08:32:45
问题 I have an IEnumerable<T>. I want to do one thing for each item of the collection, except the last item, to which I want to do something else. How can I code this neatly? In Pseudocode foreach (var item in collection) { if ( final ) { g(item) } else { f(item) } } So if my IEnumerable were Enumerable.Range(1,4) I'd do f(1) f(2) f(3) g(4). NB. If my IEnumerable happens to be length 1, I want g(1). My IEnumerable happens to be kind of crappy, making Count() as expensive as looping over the whole