permanently sorting a collection - silverlight-4.0

I have an observable collection that is exposed to the user by a collectionviewsource. One of the properties on the items in the collection is sortorder. I am trying to allow the user to permanently resort this collection and propagate the changes to the db.
I have the CVS working where I can resort the individual items as they are displayed in the listbox. Now however I have to change the item.sortorder==cvs.currentindex and i am having trouble figuring out the proper way to do this.
EDIT
evidently I was not clear enough. Sortorder is a field in my DB that is part of my object that allows the user to control position of the items as displayed in list controls. I am trying to give my user the ability to change how these items are sorted in the future by changing the value of the sortorder field to equal the current index of the displayed item.
items current sortorder value is 3.
user moves displayed listitem to position 0(ie first position)
items new sortorder=0 item with original sortorder will become 1 etc
this would be achieved by looping through the sorted CVS and making Item.SortOrder= CVS.Item.index

Databases return rows from table based typically on the order they were added to the table unless you specify an order by clause in your query. changing the order in the database is not a good idea. instead, try something such as using a parameterized query and passing in the column and direction you wish to be sorted which is retrieved from a user preference.

I figured it out.
I went back and looked at the code that I was using to change the position of the elements and I am actually using the collection itself:
private void OnDecreaseSortOrderCommandExecute()
{
int index = QuestionsCVS.View.CurrentPosition;
QuestionsViewModel item = SelectedQuestionVM;
if (item != null && index > 0)
{
SortableQuestionsVMCollection.RemoveAt(index);
SortableQuestionsVMCollection.Insert(index - 1, item);
QuestionsCVS.View.Refresh();
QuestionsCVS.View.MoveCurrentTo(item);
}
}
So I simply did this:
private void ResortSortableQuestionsVMCollection()
{
for (int i = 0; i < SortableQuestionsVMCollection.Count; i++)
{
SortableQuestionsVMCollection[i].SortOrder = i;
}
}
I dont know if this will work in every circumstance but it certainly did what I wanted for this one.

Related

sitecore mvc value of droplink

I am looking for the simplest way to get the referenced item value for a droplink field.
#Html.Sitecore().Field("Alignment")
I want to get the value of the choice, what's the best approach?
If you need to have ability to edit fields of alignment item which is chosen in 'Alignment' droplink field of context item or just show values of alignment item's fields for visitors:
#{
Sitecore.Data.Fields.ReferenceField alignmentField = Sitecore.Context.Item.Fields["Alignment"];
Sitecore.Data.Items.Item alignmentItem = alignmentField.TargetItem;
}
<div>
#Html.Sitecore().Field("Text of Alignment", alignmentItem)
</div>
This example assumes that Alignment template contains 'Text of Alignment' field.
The Droplink field stores the referenced item's ID. To retrieve this ID (providing the field is present in your current item/model):
((LinkField)Model.Item.Fields["Alignment"]).Value
To output the referenced item's name, you could do something like this:
#(Model.Item.Database.GetItem(((LinkField)Model.Item.Fields["Alignment"]).Value).Name)
But that's really ugly. The preferred approach would be to create an extension method encapsulating some of the above so you're not having to re-type that out :D
The article Extending the SitecoreHelper Class by John West shows how to extend the SitecoreHelper class to add custom field renderers, so you could end up creating a neat re-usable snippet like:
#(Html.Sitecore().ReferenceField("Alignment","Name"))
If this is in a partial view i.e. .cshtml file you can also use something like below:
Sitecore.Data.Fields.TextField alignment= Model.Item.Fields["Alignment"];
This will give you the id of the set item in the drop link , then from that id can retrieve it from the database like:
#if (!string.IsNullOrWhiteSpace(alignment.Value))
{
var setAlignment = Sitecore.Context.Database.GetItem(alignment.Value);
if (setAlignment != null && !string.IsNullOrWhiteSpace(setAlignment.Name))
{
setAlignment.Name
}
}
Personally i prefer this way as i can check if the droplink is set before trying to use the value.

How to find Trello ListId's or CardId's with Trello.NET

I'm trying to find all cards in a specific list using Trello.NET
In the examples given it's clear to to find all list names in a Board, and all Cards in a Board. However if I want to find all Cards in a specific List, as far as I can see I need ListID (can't filter by List Name) by using trello.Lists.WithId()
Knowing the List name, how can I determine what it's ListId is so I can subsequently filter for all Cards in it?
And if I would subsequently like to edit an existing Card, how can I determine the CardId.
Use LINQ and filter in-memory:
// More than one list can have the exact same name, this finds the first:
var list = trello.Lists
.ForBoard(board)
.FirstOrDefault(list => list.Name == "name of the list");
if (list != null)
{
var cards = trello.Cards.ForList(list);
// ...
}
You could use a similar technique to find a card if you know it's name: use trello.Cards.ForBoard or trello.Cards.ForList first, and then filter by name afterwards.
For cards there is another option though: trello.Cards.Search. For example:
var cards = trello.Cards.Search("name of the card");
The search will be performed on the server in this case.

How to write a RavenDB index as a prefiltered list of documents, not searchable

I want to get a list of users filtered by a property (string) being null or empty.
I've created an index for this but I'm not sure if my way of implementing it is the right way to do it.
public class Users_Contributors : AbstractIndexCreationTask<User>
{
public Users_Contributors()
{
Map = users => from user in users
where user.ContributionDescription != null && user.ContributionDescription != ""
select new {};
}
}
So I just want raven to "prepare" the list of users for me. I'm just gonna get all user objects out of that index with no additional filtering/where criterias at query time.
Is the above code the way to go or can I achieve the same in a better way? Im feeling Im missing something here. Thanks!
This will work just fine. The result would be that the index only contains users that have something in their ContributionDescription field.
If you want to make it slightly easier to read, you can use string.IsNullOrEmpty, but that won't have any impact on performance.
Map = users => from user in users
where !string.IsNullOrEmpty(user.ContributionDescription)
select new {};
It probably feels strange because of the empty object at the end, but that just defines the index entries. If you aren't sorting or filtering by any other field, then using an empty object is just fine. Keep in mind that the __document_id entry gets created regardless of what fields you map.

GridView - Limit group items

My data is a list of groups, each having an undefined number of items. For the Hubpage I want to limit the items for each group to a specific number. Do I have to create a second collection with only those ten items or is there a XAML way to limit the group items to the top n?
If you're binding to an IEnumerable off of an ObservableCollection (or other Collection type), use the Take extension method to return the Top N. In the example template, you would do something like ...
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data
var sampleDataGroups = SampleDataSource.GetGroups((String)navigationParameter);
this.DefaultViewModel["Groups"] = sampleDataGroups.Take(5);
}
Both approaches are possible, but with the caveats. If you would like to use standard XAML controls you would have to effectively create a new collection with as many items as you would like to show. Take(N) for example would create new collection with N elements. You could also create your own custom control that will limit amount of visible elements. I general new collection approach is the most common one.
You can do the following in the 'Grouped' event of the grid:
if(xamDataGridHotworkS_Flow.FieldLayouts[0].SortedFields.Count > 1)
{
xamDataGridHotworkS_Flow.FieldLayouts[0].SortedFields.RemoveAt(1);
}
If there is grouping on the grid, remove the next grouping supplied by the user.

Check if property exists in RavenDB

I want to add property to existing document (using clues form http://ravendb.net/docs/client-api/partial-document-updates). But before adding want to check if that property already exists in my database.
Is any "special,proper ravendB way" to achieve that?
Or just load document and check if this property is null or not?
You can do this using a set based database update. You carry it out using JavaScript, which fortunately is similar enough to C# to make it a pretty painless process for anybody. Here's an example of an update I just ran.
Note: You have to be very careful doing this because errors in your script may have undesired results. For example, in my code CustomId contains something like '1234-1'. In my first iteration of writing the script, I had:
product.Order = parseInt(product.CustomId.split('-'));
Notice I forgot the indexer after split. The result? An error, right? Nope. Order had the value of 12341! It is supposed to be 1. So be careful and be sure to test it thoroughly.
Example:
Job has a Products property (a collection) and I'm adding the new Order property to existing Products.
ravenSession.Advanced.DocumentStore.DatabaseCommands.UpdateByIndex(
"Raven/DocumentsByEntityName",
new IndexQuery { Query = "Tag:Jobs" },
new ScriptedPatchRequest { Script =
#"
this.Products.Map(function(product) {
if(product.Order == undefined)
{
product.Order = parseInt(product.CustomId.split('-')[1]);
}
return product;
});"
}
);
I referenced these pages to build it:
set based ops
partial document updates (in particular the Map section)