enumerable

LINQ inner join betwenn Enumerable and DB Table

I'm trying to determine which records to delete from a database when a user submits a form. The page has two CheckBoxList one representing the records before modification and one after. I can easily get the selected values that need to be deleted like this... //get the items not selected that were selected before var oldSelectedItems =...

Random array using LINQ and C#

I was reading an article on MSDN Magazine about using the Enumerable class in LINQ to generate a random array. The article uses VB.NET and I'm not immediately sure what the equivalent is in C#: Dim rnd As New System.Random() Dim numbers = Enumerable.Range(1, 100). _ OrderBy(Function() rnd.Next) ...

Why is Enumerable.Range faster than a direct yield loop?

The code below is checking performance of three different ways to do same solution. public static void Main(string[] args) { // for loop { Stopwatch sw = Stopwatch.StartNew(); int accumulator = 0; for (int i = 1; i <= 100000000; ++i) { accumulator +...

Executing a certain action for all elements in an Enumerable<T>

I'm getting started with C# 3.0 and LINQ, and I can't find a feature that to me is obvious, it's gotta be there. I have an Enumerable, and I want to execute something for each instance of it. Something like... string[] Names = ...; Names.each(s => Console.Writeline(s)); or Names.each(s => GenHTMLOutput(s)); // (where GenHTMLOutput c...

What is an interface that specifies an enumerable Windows control?

I have a method that validates a combo box control like so: Public Function ValidateComboBox(ByVal objMessageMode As Errors.MessageMode, ByVal cboInput As ComboBox) As Boolean Dim blnValidated As Boolean 'no value--invalidate' If cboInput.SelectedValue Is Nothing Then Errors.InvalidateField(cboInput, Errors.errFiel...

What is the best way to modify a list in a foreach?

Hello, a new feature in C# / .NET 4.0 is that you can change your enumerable in a foreach without getting the exception. See Paul Jackson's blog for info on this change. So I'm asking: what is the best way to do this: foreach(var item in Enumerable) { foreach(var item2 in item.Enumerable) { item.Add(new item2) } } ...

Sort Objects by Boolean values in Ruby

My apologies if this has been answered before or is obvious...did some searching here and on the Goog and couldn't find an answer. I'm looking to sort an array of Providers by price and whether they are a preferred_provider? (true or false) For instance in array p of Providers... p1.price == 1, p1.preferred_provider? == false p2.price...

How can I make DataTable enumerable ?

I cannot use AsEnumerable() on DataTable, I'm using C# 3 but I'm just targeting 2.0 framework (LINQ capability is courtesy of LINQBridge). Is there any way I can make DataTable enumerable without using Select() ? bool isExisting = (bdsAttachments.DataSource as DataTable).Select().Any(xxx => (string)dr["filename"] == filename); [EDIT]...

So maybe I'm not getting the idea in Ruby but I have a question about Enumerables inject.

The |m,k| thing kind of throws me off. Does this have anything to do with order of precedence? m standing for 0 (or 1 in some languages) and k for the last in the Array/Hash whatever? So why do people put a number in .inject()? Alternatively, is there an easy way to learn how to use this, and exactly what it's value is? Judging from th...

How can I get a list from a Ruby enumerable?

I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby? I know of : elements = [] enumerable.each {|i| elements << i} I also know of the inject alternative. Is there some ready available method? ...

Conditional summarizing via inject

How to get the index of item in: my_array.inject {|rs,item| rs += item} I need to summarize all except the i-th element. ...

How can I use index or rindex with a block in Ruby?

Is there any Array or Enumerable built-in that allows me to search for an element using a block, and return its index? Something along the lines of : ar = [15,2,33,4,50,69] indexes = ar.find_indexes {|item| item > 4 == 0} # indexes will now contain 0,2,4,5 It would be very easy to add my own, but I'd like to know if this already exis...

Default Logical Sort on Resultset Array RubyonRails

I've add a custom sort to an ActiveRecord model by defining a method like this: class MyClass < ActiveRecord::Base belongs_to :parent_model #this would be the many in a has_many relationship def <=>(other) self.att <=> other.att end end Suffice it to say, the logic actually being employed in the comparison is a bit more com...

Generate and repeat number in C#

I want to generate an array that has 144 number from 1->36 in random order (so each number is repeated 4 times). Can we use Enumerable.Repeat and Enumerable.Range to do that. If yes than please explain to me how?. Thanks you very much. ...

Does Enumerable.Repeat() do a deep copy ?

Hi all, If I use the following: var myList = Enumerable.Repeat(myCustomObject, 2); Will the Second element in the list be a deep copy of the first one? Note: myCustomObject can be any Object Edit: Could you also please let me know the potential use of Enumerable.Repeat when dealing with custom objets? Thanks ...

Does Enumerable.ToDictionary only retrieve what it needs?

I'm using Enumerable.ToDictionary to create a Dictionary off of a linq call: return (from term in dataContext.Terms where term.Name.StartsWith(text) select term).ToDictionary(t => t.TermID, t => t.Name); Will that call fetch the entirety of each term, or will it only retrieve the TermID and the Name fields from my data...

Persist an enum wrapped in an interface with hibernate?

I'm using the latest version of Hibernate with Java. I've got three enum types that are being used as property names, each of the three property names go with different objects. There are different sets of property names for each object. Each property name is stored in a map with it's respective property value object, the property value ...

Property with Enumerable or list

I'm playing around with LINQ and related subjects and was wondering about the following. I've 2 methods of getting a Fibonacci sequence. I started with: public static IEnumerable<int> Fibonacci { get { int i = 0; int j = 1; int temp = 0; while (true) { ...

Sort a collection of objects by number (highest first) then by letter (alphabetical)

I'm building a widget to show medal counts for the Olympics. I have a collection of "country" objects, where each has a "name" attribute, and "gold", "silver", "bronze" for medal counts. List should be sorted: 1. First by total medal count 2. If same medals, sub-sort by type (gold > silver > bronze, ie. two golds > 1 gold + 1 silver) 3....

Why doesn't each_slice work?

I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found. I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0] API: http://ruby-doc.org/core/classes/Enumerable.html#M003142 Example: (1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `eac...