Use findAll and then Sort in vb.NET listOf - vb.net

I'm trying to get specific elements of a listOf structure with the function findAll plus a lambda function, and then sort that result and keep that sorting of those elements saved in the list. The structure of the listOf has an id and an age, so what I want is get all the items with id=0 and then sort the ages of that result, keeping that modification in the variable.
This is what I've tried, but it doesn't work
list.FindAll(Function(p1) p1.id = 0).Sort(Function(p1, p2) p1.age > p2.age)

Your line of code doesn't work, because FindAll returns a new list with the items found, and you don't assign that new list to the list variable. Also, you can't chain the call to Sort because this function changes the underlying list and doesn't return anything. To use FindAll and Sort together you will need two statements:
list = list.FindAll(Function(p1) p1.id = 0)
list.Sort(Function(p1, p2) p1.age.CompareTo(p2.age))
It would be easier to use Linq to do this, as you can always chain expressions with Linq, and the sorting is easier to do. But you need to assign the result to the list again:
list = list.Where(Function(p1) p1.id = 0) _
.OrderBy(Function(p1) p1.Age).ToList()

Related

Redis: Sort keys from lists by number of elements

I have a redis-store that has multiple lists, indexed by list:name_of_list. Is it possible to retrieve the keys of all lists sorted in a way that the list with highest number of elements is the first and the list with the lowest number of elements is the last?
Not directly, you will need to get the lengths of the lists, then sort them, then get the elements of each list. You can do that in your client, or if you prefer, you can do that in a Lua function that will act as sort of a stored procedure.
The gist of it, in Python/pseudo code will look like:
lists = ['list1', 'list2', 'list3']
lengths = [client.llen(lst) for lst in lists]
# zip and sort the lists and their lengths
keys_and_lengths = sorted(zip(lists, lengths), key=lamda pair: pair[1], reverse=True)
# Get the members, you can chain them in various ways. You can also use a pipeline
members = [client.lrange(key, 0, -1) for key, _ in keys_and_lengths]

GroupBy in Deedle

I want to group my dataframe by certain columns and then iterate over the groups like I can easily do using the LINQ .GroupBy().
var groupedByTags = dataFrame.GroupRowsUsing((i, series) => GetTags(series));
// GetTags extracts my groupKey
does do the grouping, but I couldn't figure out how to iterate over the groups. I think the GetByLevel function might help, but it seems I need to know the groups first before I can use it.
Also I'm using the C# version of deedle.

Grouping ActiveRecord objects in batches

I'm trying to return a group of products in my rails so that I can seperate them when I iterate in my view.
For example, if I have 13 products, I want the block in the view to put the first 7 on one row, break and put the next six on the next row (I'm using css to put a shelf under the products).
I've been experimenting with find_in_batches, but can't seem to get this to work (not even sure it's the appropriate method).
#shelves = Product.find_in_batches(:batch_size => 7) { |products| products }
I usually use group_by when I want to group based on a date, for example -- is there a way to use group_by to group by counts, instead of model attributes?
find_in_batches.map will give you a no block error. What you actually want is:
#shelves = Product.all.in_groups_of(7)
And if you'd like the last group to not have extra nil objects padding it out, try:
#shelves = Product.all.in_groups_of(7, false)
Of course, you'll want to replace all with a more sensible scope so you're not loading your entire list of database objects into memory :)
You want an array of batches. Just mapping over the batches should do it.
#shelves = Product.find_in_batches(:batch_size => 7).map{|batch| batch}

How to get minimum value from an of type list generated from Linq to SQL

I'm returning a list of database records;
Dim rsPublicChilds As System.Data.Linq.ISingleResult(Of spGetPublicObjectsResult) = Nothing
rsPublicChilds = dc.spGetPublicObject(slintLoginID, lintLanguageID, lintObjectID, lintObjectTypeID, lstrSEOURL, lstrValid)
I get an enumerable list of rsPublicChildObjects that I then convert to an array;
Dim larr_PublicChild As IEnumerable(Of spGetPublicObjectsResult) = rsPublicChilds.toArray()
That then gives me easy access to an array of the objects, so I can then do;
larr_publicchild(0).colMyValue
etc.etc
I'd like to get the minimum value of colMyValue (or any other property of the object that's been created for me) but I can't quite see how to get there.
thanks
Andy
You can write
someCollection.Min(Function(x) x.SomeProperty)

How Do I step through an IList one field at a time?

I'm using the Dynamic LINQ library code sample to dynamically return some data. The number of columns I'm returning is variable depending on user input. I'm enumerating the IQueryable into an IList.
What I need to do is step through the Ilist one field at a time. I can get one row at a time by iterating through the IList's rows but I can't for the life of me pull one field out of the collection.
For example, here I'm returning two columns (hard coded for testing but in prod it will be variable depending on what fields the user chooses):
Dim Dynq = dc.dt _
.Where("RUN_ID = """ & runNumber & """ and Upper_Pressure > 95") _
.OrderBy("Upper_Pressure") _
.Select(" new (Run_ID,Process)")
Dim something = DirectCast(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(Dynq.ElementType), Dynq), IList)
Now I can pull a field out of the Ilist if I know the column name with something like:
something.Run_ID.ToString
but I wont know the columns I'm using until runtime and dynamically inserting it in a variable that's set at runtime doesn't work.
So in Summary I have a Ilist that looks something like this
1 | Auto
2 | Auto
3 | Manual
4 | Manual
and I'd like a way to return
1
and then return
Auto
and then
2
etc...
I would greatly appreciate the help of those more learned than I in this.
Your question is a little confusing, but I think you want to flatten your return set to return specific columns as elements in order by column then row...
One method, you could use is the SelectMany operator.
For example (sorry in C# as my brain is turning one-tracked!):
// Find flattened list of some explicitly specified property values...
var flattened = something.SelectMany(e => new [] { e.Run_ID.ToString(), e.Process.ToString() });
Not sure if that's what your after, but it could be a step in the right direction.