Using map-reduce to ordering list - mapreduce

Have a piece of code like:
public class Idea
{
(...)
public IList<IdeaSupporter> Supporters { get; set; }
}
public class IdeaSupporter
{
(...)
public int Tokens { get; set; }
}
Try to order ideas by supporters tokens. I know i must use map-reduce but i quite new at it. Anyone knows how should using map-reduce in that case correcty looks?

The index should look like:
from idea in docs.Ideas
select new
{
SumOfSupportersTokens = idea.Supporters.Sum(x=>x.Tokens)
}
Note that this isn't a map/reduce index, it is a simple map index that outputs the sum.
You can then order on that.

Related

Grouping items based on last added item in set

I have an app where I store debug information from several sources. The data is stored in a class like the following:
public class DebugMessage
{
public string ApplicationName { get; set; }
public string Details { get; set; }
public string Id { get; set; }
public DateTime OccurredOn { get; set; }
public IList<string> Tags { get; set; }
public string TextMessage { get; set; }
public MessageTypes Type { get; set; }
public IDictionary<string, string> Metadata { get; set; }
public int Count {get;set;}
public bool Same(DebugMessage other){...}
}
Now, I already have setup indexes and maps/reduce for each item I need. What I would like to do now is the following:
When ADDING a new item to the collection, If this items "looks the same" (by calling the Same method on the item and passing in the last added item in the collection), I would like to just update the last added item and do not add a new item. If the items are not the same, I would like to add it to the collection.
I guess I can do this with some kind of Map/Reduce, but I can't wrap my head around this. I'm new to Raven and don't know how to do the above (or even if that's possible).
Any directions?
You need to move your in the Same method to the map/reduce index, and group based on the values that make you consider the two things to be the same.
Alternatively, query for similar debug message, and update the result.

neo4jClien Create node with paramaters using a List<Properties>

I am trying to create a node that takes a list of properties and uses the list object to create those properties. Is there any way to do this?
public List<PropertiesModel> node_properties;
public class PropertiesModel{
public string propertyName { get; set; }
public string propertyValue { get; set; }
}
then when I pass this on to:
client.Cypher
.Create("(n:Label {node})")
.WithParam("node", node_properties)
.ExecuteWithoutResults();
I get the following error when I run this:
CypherTypeException: Collections containing mixed types can not be stored in properties.
I am guessing since this is a list, containing a model made of strings it does not like it. Is there another way to go about building dynamic models of paramaters? I thought about IDictionary but it seems I may have issues mapping directly from a JSON post into a IDictionary.
Thanks
ok, so I am feeling slow. Just answered my own question.
I had
class nodeModel{
List<PropertiesModel> props {get; set;}
}
class PropertiesModel{
public string propertyName { get; set; }
public string propertyValue { get; set; }
}
so I needed to do:
client.Cypher
.Create("(n:Label {node})")
.WithParam("node", node_properties.props)
.ExecuteWithoutResults();
I had to step into the list basically with the .props
but this was not giving me my intended results. Say my list contained 2 properties that I was intending to have attached to 1 node. This actually created a node per property. To solve this, I mapped this over to a IDictionary.
IDictionary<string, string> map = node_properties.props.ToDictionary(p=>p.propertyname, p=>p.propertyValue);
then I changed the .WithParams part of the query statement to:
.WithParams(map)
And The intended result was 1 node made up of a list of properties.

Windows Phone ListView object with list

I have a object which contains 5 list of object, and I would like to display this object in only one listview, I tried a lot of different things, like groups, but I can't find a solution. This is my object :
public class SearchResult
{
public List<User> artist { get; set; }
public List<User> user { get; set; }
public List<Music> music { get; set; }
public List<Album> album { get; set; }
public List<Pack> pack { get; set; }
}
How can I bind 5 list and display the information a need for each one by using a Template.
Thanks.
So you are looking for something like the Search results in the XBox Music app?
It is probably just multiple ListViews, but if you really want all of them in a single ListView, you have to:
Put your lists together in a ListOfLists (List>), so you can use it with CollectionViewSource.
Or: Implement IGrouping on your SearchResult.
Then you can just use the GroupStyle on the Listview and ItemTemplateSelector will help you use different Templates on each Type.
So you will have to change your data structure if you want to bind it.

many-to-one ForeignKeys in SQL CE db developed with EF Code First

This is my first foray in either SQL CE or EF, so I may have a lot of misunderstandings. I've searched a lot of blog entries but still can't seem to get this right.
I have an MVC3 web site for registrations for a race we're running. I have a RaceEvents table, and a Runners table, where each RaceEvent will have many runners registered it for it, i.e., Many-to-One. Here are the POCO's with extraneous data stripped out:
public class RaceEvent
{
[Required]
public int Id { get; set; }
public virtual ICollection<Runner> Runners { get; set; }
}
public class Runner
{
[Required]
public int Id { get; set; }
[Required]
public int RaceEventId { get; set;}
[ForeignKey("RaceEventId")]
public RaceEvent RaceEvent { get; set; }
}
Which, as much as I can figure out, ought to work. As I understand it, it should figure out by convention that RaceEventId is a foreign key to RaceEvents. And if that's not good enough, I'm telling it with the ForeignKey attribute.
However, it doesn't seem to be working. Whenever I insert a new runner, it is also inserting a new entry in the RaceEvents table. And when I look at the table diagram in ServerExplorer, it shows two gold keys at the top of the Runners table diagram, one for Id, identified in the properties as a PrimaryKey, and the other for RaceEventId, not identified as a PrimaryKey, but indicated in the properties to be for table Runners, rather than for table RaceEvents. I would expect a gold key for Id, but a silver ForeignKey for RaceEventId.
FWIW, I don't really care about the ICollection in the RaceEvent, but the blog entries all seemed to imply that it was necessary.
Can anybody help me get this right?
Thanks.
Ok,
Sorry I did not read your question in enough detail. In our project this is how we would represent what your doing. I looked in SSMS and it is not showing said grey key, but it does not create a race event every time you add a runner. Although you do need to make sure when you create a runner that you set the race event property.
public class DB : DbContext
{
public DB()
: base("Data Source=(local);Initial Catalog=DB;Integrated Security=True")
{
}
public IDbSet<Runner> Runners { get; set; }
public IDbSet<RaceEvent> RaceEvents { get; set; }
}
public class RaceEvent
{
[Key]
public int RaceEventID { get; set; }
}
public class Runner
{
[Key]
public int RunnerID { get; set; }
[Required]
public virtual RaceEvent RaceEvent { get; set; }
}
Any question let me know.
You need to override the model creating in the DbContext. Below is a sample for AnsNet_User & AspNet_Roles N:N relationship
protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
dbModelBuilder.Entity<aspnet_Users>().HasMany(a => a.aspnet_Roles).WithMany(b =>
b.aspnet_Users).Map(
m =>
{
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
m.ToTable("aspnet_UsersInRoles");
});
base.OnModelCreating(dbModelBuilder);
}

Need to Seriazlize List<object>, but FXCop complains "Do not expose generic lists"

I have an object that I need to serialize. The object contains several properties, including a List. FXCop is complaining that I should not expose generic lists, and I get that, however, due to the fact that I can't specify an interface based property on an object that I want serialized I'm not sure where to turn next.
Any thoughts?
BTW, I'm using XMLSerialization, but that's not a requirement.
I took FxCop's suggestion and wrapped my list in a Collection. This blew some of my code out of the water, but a after a few adjustments I was up and running again.
Here's some code showing before and after:
Before:
public class PersistentDataView
{
public string Title { get; set; }
private List<object> Inputs { get; set;}
}
After:
public class PersistentDataView
{
private List<object> _inputs;
public string Title { get; set; }
public Collection<object> Inputs
{
get
{
if (_inputs == null)
_inputs = new List<object>();
//Wrap the private field into a collection.
return new Collection<object>(_inputs);
}
}
}