Querying instances contained in a list - list

I have created a list (dd) which contains instances of a class. I want to be able to perform queries on some of its attributes (eg. finding the smallest numeric value), but cannot find a way to do this using the functionality of the class (I feel like going back to analysing the data by column). How do I access the values in one of the attributes when my instances are now on a list? Thanks!

Assuming you're using C# and having a class like this
class Foo
{
public double Bar { get; set; }
}
then LINQ is exactly what you are looking for
List<Foo> list = GetList();
var filtered = list.Where(p => p.Bar > 23.0);
Have a look at these examples:
https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Related

Objectify - Order by reference

is it possible in Objectify to order by a field of a referenced object?
Example:
public class Foo {
#Id private long id;
#Index Ref<AnObject> ref;
}
public class AnObject {{
#Id private long id;
#Index name;
}
Then I try to do this query and it's not working:
Query<Foo> query = ofy().load().type(Foo.class)
.limit(Constants.DATASTORE_LIMIT)
.order("ref.name");
Thanks.
I'm afraid not. This is not something supported by the datastore. It can't do joins.
There are basically two ways around that limitation:
Denormalize and put an indexed copy of the 'name' data in your Foo object. You can do this somewhat automatically with an #OnSave method. The downside is that you need to remember to update the data in both places if it changes.
Do the work of the query planner yourself. Query for the relevant AnObjects, then query for the relevant Foos.

Create Lists from List based on Matching Object Properties

I have a list of objects which contain several properties. I am looking to break the list into several lists composed of objects which posses the same sub-property.
Here is a hypothetical example, say I have a list of Cars. Each Car has properties: id, manufacturerId and color.
I would like to create lists of Cars for those with matching manufacturerId properties.
I have tried using list.where within list.forEach to create new sub-lists, however I get duplicate lists because when the property I am comparing against (A) matches with another property (B), I get another sub-list when:
B = A.
You can use groupBy from package:collection
var carsByManufacturer = groupBy(cars, (car) => car.manufacturerId);
This will create a Map where the keys are the manufacturerID, and the values are lists of cars with that manufacturer.
Then do,
for (final manufacturerEntry in carsByManufacturer) {
final key = manufacturerEntry.key;
final value = manufacturerEntry.value;
final listOfCarsFromSameManufactuer = List.from(entry.value);
...
}
You will now have lists called: listOfCarsFromSameManufactuer.

Sort 'Days' Items in Sitecore according to days of week

Is there any way to sort the following items according to the days of week.
In C# I can do something like this:
string [] initialArray = {"Friday", "Monday", ... } ;
string [] sortedArray = initialArray.OrderBy(s => Enum.Parse(typeof(DayOfWeek), s)).ToArray() ;
But I don't know how can I achieve this kind of functionality with Sitecore.
If what you really care is to display the days sorted on the front-end regardless of how they are organised in the Content Editor then simply sort them in code before you display them, e.g.
using System.Linq;
var openingHours = Sitecore.Context.Item.Children
.OrderBy(s => Enum.Parse(typeof(DayOfWeek), s.DisplayName));
If you want to sort them in the Content Editor then you need to create a custom sorter. Sitecore Climber has provided links, but for this specific example you can use:
using Sitecore.Data.Comparers;
using Sitecore.Data.Items;
public class DayOfWeekComparer : Comparer
{
protected override int DoCompare(Item item1, Item item2)
{
var x = (int)Enum.Parse(typeof(DayOfWeek), item1.DisplayName);
var y = (int)Enum.Parse(typeof(DayOfWeek), item2.DisplayName);
return x.CompareTo(y);
}
}
Then in the core database create an item of item type /sitecore/templates/System/Child Sorting under /sitecore/system/Settings/Subitems Sorting and set the type to your class.
You should set the Subitem Sorting on the Standard values of a template. In this instance it looks like you have a simple Folder template, so you would need to create a more specific template for your Opening Hours folder. Even so, the user can still decide to re-order the items OR change the default sort for that folder. The only guaranteed way to force the output is by sorting before you render, i.e. the first bit of code.
please check next articles:
http://firebreaksice.com/how-to-sort-sitecore-items-in-the-content-editor/
http://sitecore.alexiasoft.nl/2009/08/04/sorting-sitecore-items/
http://sitecoreblog.blogspot.in/2010/11/change-default-subitems-sort-order.html

Sitecore/Glass Mapper: how to return query in order of content tree

We have a site with several queries that query the direct children of a content item. We simply want just the children sorted by the order they appear in the content tree.
We're using Glass Mapper and our collection properties look like this:
[SitecoreQuery("/sitecore/content/Global/Team Members/Categories/*")]
public IEnumerable<ICategory> Categories { get; set; }
The above property Categories returns the child items in what seems to be alpha order, but in some cases it seems a bit random.
Any idea how to set up the query to pull in the order of the content tree?
Scott,
I believe that Glass is using fast query for these SitecoreQueries which is why you aren't getting any predictable sort order. Unfortunately the fast query does not allow sorting in the query syntax. You will see the same if you put your query into the XPath Builder in Sitecore's Developer Center by using this syntax:
fast:/sitecore/content/Global/Team Members/Categories/*
I think the quickest way to resolve this is to just add Sitecore's __Sortorder field to your ICategory definition.
[SitecoreField("__Sortorder"),]
string Sortorder { get; set; }
You could then add another property to your model that returns the sorted version of this.
public IEnumberable<ICategory> SortedCategories { get { return Categories.OrderBy(s=>s.Sortorder); } }
Keep in mind you can also use the [SitecoreChildren] decorator to get the children of the current item. I'm not sure if that is actually what you are after, but that decorator will actually return your items in the correct order per Sitecore's SortOrder.

converting linq query to icollection

I need to take the results of a query:
var query = from m in db.SoilSamplingSubJobs where m.order_id == id select m;
and prepare as an ICollection so that I can have something like
ICollection<SoilSamplingSubJob> subjobs
at the moment I create a list, which isnt appropriate to my needs:
query.ToList();
what do I do - is it query.ToIcollection() ?
List is an ICollection. You can change you query.ToList() code to the following.
query.ToList() as ICollection<SoilSamplingSubJob>;
You question sounds like this query is returned as a function result. If this is the case remember that the Linq to SQL objects are connected by default so you will need to manage where your database context get opened and closed. Alternatively you can create DTOs (Data Transfer Objects) that hold the data you want to use in the rest of your program. These objects can fit into your object hierarchy any way you want.
You can also create these DTOs as part of the query.
var query = from m in db.SoilSamplingSubJobs where m.order_id == id
select new SubJobDTO {
OrderNumber = m.order_id
};
return query.ToList() as ICollection<SubJobDTO>;
Since, query implements IEnumerable, you can pass it to the constructor of the collection of your choice. ICollection is an interface, implemented by several classes (including List<T>, which ToList returns) with different performance characteristics.
With slightly different syntax, you can do something like this as well that can cut down on what goes into the ICollection. This type of approach is great for Angular and MVC when you have a huge table but only want to load some props into the cshtml page:
ICollection<SoilSamplingSubJob> samples = dbContext.GetQuery().Where(m => m.order_id == id)
.AsEnumerable().Select(s => new
{
Id = s.order_id,
CustomProperty = s.some_thing
}).ToList() as ICollection<SoilSamplingSubJob>