Related
I have a class with a dictionary defined as a private member :
Dictionary<int, (string, string)> arenaIdToSetAndNumber = new Dictionary<int, (string, string)>()
{
{ 70506, ("c16", "337") },
{ 70507, ("c16", "340") },
{ 70508, ("c16", "343") },
{ 70509, ("c16", "346") },
{ 70510, ("c16", "349") },
};
While debugging, I get to an item corresponding to key 70506, I see this with 2 watches:
I try doing var test = arenaIdToSetAndNumber[c.grpId].Item1 and test is set to null just as seen in the second watch! I don't understand why
The debugger and the watcher are not able to infer what is Item1 from the indexer operator [], thus will give you null in the watch. But once you run the code, it will just work fine for reading purpose. For writing purpose instead, you need to take out the whole tuple, edit it and reinsert in the dictionary:
static void Main(string[] args)
{
Dictionary<int, (string, string)> arenaIdToSetAndNumber = new Dictionary<int, (string, string)>()
{
{ 70506, ("c16", "337") },
{ 70507, ("c16", "340") },
{ 70508, ("c16", "343") },
{ 70509, ("c16", "346") },
{ 70510, ("c16", "349") },
};
var myTuple = arenaIdToSetAndNumber[70509];
myTuple.Item1 = "c18";
arenaIdToSetAndNumber[70509] = myTuple;
//System.Console.WriteLine(arenaIdToSetAndNumber[70509].Item1); // This prints c18
}
Otherwise, in one line, just recreate the whole tuple:
arenaIdToSetAndNumber[70509] = ("c18", arenaIdToSetAndNumber[70509].Item2);
All of this because the ValueTuple is a struct. Similar question here
This does not use tuples but solves your problem. Since you want to read the values create an immutable class, use properties to retrive the values.
public class Contents
{
private readonly string leftValue;
private readonly string rightValue;
public Contents(string aLeftValue, string aRightValue)
{
leftValue = aLeftValue;
rightValue = aRightValue;
}
public string LeftValue => leftValue;
public string RightValue => rightValue;
}
Modify your code to use the new class.
Dictionary<int, Contents> arenaIdToSetAndNumber = new Dictionary<int, Contents>()
{
{ 70506, new Contents("c16", "337") },
{ 70507, new Contents("c16", "340") },
{ 70508, new Contents("c16", "343") },
{ 70509, new Contents("c16", "346") },
{ 70510, new Contents("c16", "349") },
};
And you can test it with this.
var content = arenaIdToSetAndNumber[70506];
string leftValue = content.LeftValue;
string rightValue = content.RightValue;
Hope this solves your problem.
I want to create a class that is a Custom List. So I have done
public class Set1 : List<Dictionary<string, string>>
{
public Set1() : base(List<Dictionary<string, string>>)
{
List<Dictionary<string, string>> mySet = new List<Dictionary<string, string>>()
{
new Dictionary<string, string>()
{
{"first_name","John"},
},
new Dictionary<string, string>()
{
{"last_name","Smith"},
},
};
base(mySet);
}
}
But this does not compile. What am I doing wrong please? Thanks in advance.
You can't call a base/alternate constructor from within the method in C# like you can in some other languages.
However, you don't need to call the base constructor in this case - you can just do:
public Set1()
{
this.Add(
new Dictionary<string, string>()
{
{"first_name","John"},
}
);
this.Add(
new Dictionary<string, string>()
{
{"last_name","Smith"},
}
);
}
If you really want to call the base constructor, though, you'll have to inline the list creation in the declaration:
public Set1()
: base( new List<Dictionary<string, string>>
{
new Dictionary<string, string>()
{
{"first_name","John"},
},
new Dictionary<string, string>()
{
{"last_name","Smith"},
}
}
)
{
// nothing more to do here
}
but that creates a list, only to have the constructor copy the items into the list, increasing your memory usage for a short time.
Here is the code you're looking for
new Dictionary<string, string>() {
{"first_name","John"}, {"last_name","Smith"},
}.
You don't have any need to inherit from List here. What you wanted was an instance of some collection. A class is a general template for data and behaviour, not something you define to hold the specific information for John.
Even better, create a class for the apprioriate thing (a person), and create an instance of a List<Person>
public class Person
{
public string Forename {get;set;}
public string Surname {get;set;}
}
///
var people = new List<Person>() { new Person("John", "Smith") };
We are building a nested UI view for a customer and require a solution to merge 2 dictionary fields into a single consolidated dictionary as well as make the keys searchable as though they are field names. I managed to create a Map/Reduce index using the techniques mentioned in http://ravendb.net/docs/2.5/client-api/advanced/dynamic-fields and https://groups.google.com/forum/#!msg/ravendb/c0HdJT-yyvQ/qvkVRrZfvmgJ.
public class ViewFolderResultWithIndividualProperties
{
public string EntryId { get; set; }
public List<KeyValuePair<string, string>> MetadataProperties { get; set; }
public List<KeyValuePair<string, string>> NamedProperties { get; set; }
public List<KeyValuePair<string, string>> Properties { get; set; }
public string FlattenedProperties { get; set; }
public string _ { get; set; }
}
MetadateProperties – it is a dictionary of Key, Value pairs. For e.g.,
"MetadataProperties": [
{
"Key": "JobName",
"Value": "one job"
},
{
"Key": "Organization",
"Value": "foo"
}]
NamesProperties – it is a dictionary of known Key, Value pairs. For e.g.,
"NamedProperties": [
{
"Key": "Tags",
"Value": ""
},
{
"Key": "Name",
"Value": "file-184"
},
{
"Key": "Uploader",
"Value": "rmani#transper.com"
},
{
"Key": "FileType",
"Value": "Jpg"
},
{
"Key": "Language",
"Value": "English"
}]
Properties – It is a merged Dictionary that contains the Key, Value pairs from both MetadataProperties and NamedProperties.
FlattenedProperties and _ are the properties that contains the flattened field values of “NamedProperties” and “MetadataProperties” respectively. I can’t figure out a way to flatten a computed Property like “Properties” (which combines both MetadataProperties and NamedProperties dictionaries). I tried Concat
Here’s the Index creation code:
public class PortalEntryViews_DocumentIdSplitIndex : AbstractIndexCreationTask<PortalEntry, ViewFolderResultWithIndividualProperties>
{
public PortalEntryViews_DocumentIdSplitIndex()
{
Map = portalEntries => from portalEntry in portalEntries
select new
{
EntryId = portalEntry.Id,
MetadataProperties = portalEntry.MetaData.Select(t => new KeyValuePair<string, string>(t.Key, t.Value)).ToList(),
NamedProperties = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("Tags", string.Join(",", portalEntry.Tags.Where(t => !t.IsInternal).Select(t=>t.Name))),
new KeyValuePair<string, string>("Name", portalEntry.Name),
new KeyValuePair<string, string>("Uploader", portalEntry.Uploader),
new KeyValuePair<string, string>("FileType", portalEntry.FileType),
new KeyValuePair<string, string>("Language", portalEntry.Language),
new KeyValuePair<string, string>("Name", portalEntry.Name) },
Properties = new List<KeyValuePair<string, string>>(),
FlattenedProperties = "",
_ = ""
};
Reduce = results => from result in results
group result by new { result.EntryId, result.MetadataProperties, result.NamedProperties, result.FlattenedProperties, result._ } into g
select new
{
EntryId = g.Key.EntryId,
MetadataProperties = g.Key.MetadataProperties,
NamedProperties = g.Key.NamedProperties,
Properties = g.Key.MetadataProperties.Concat(g.Key.NamedProperties).ToList(),
FlattenedProperties = g.Key.NamedProperties.Select(f => CreateField(f.Key, f.Value)),
_ = g.Key.MetadataProperties.Select(t => CreateField(t.Key, t.Value, true, true))
};
}
}
When I run a query like “Language:English” from RavenDb Explorer directly, it works and returns a projection. Whereas when I run the same query using LuceneQuery from within my C# code:
var entries =
session.Advanced.LuceneQuery<ViewFolderResultWithIndividualProperties>(
"PortalEntryViews/DocumentIdSplitIndex")
.WhereEquals("Language", "English").ToList();
I get this error:
Raven.Imports.Newtonsoft.Json.JsonSerializationException : Could not read value for property: FlattenedProperties ----> Raven.Imports.Newtonsoft.Json.JsonReaderException : Error reading string. Unexpected token: StartArray
My ultimate goal is to flatten the combined dictionary i.e. Properties field into a single field using CreateField() that can be searched using the keys as though they are field names. But, if I use a call like this:
Properties = g.Key.MetadataProperties.Concat(g.Key.NamedProperties).ToList().Select(t => CreateField(t.Key, t.Value, true, true)), it seems to run but when you look at the index from Ravendb Explorer, it shows the actual error:
Stage: Indexing Section:Reduce Description: ‘System.Collections.Generic.List’ does not contain definition for ‘Select’
Right now, I’m only able to flatten only one Dictionary (MetadataProperties) into that “_” field in reduce section, which works from both Ravendb Explorer and from C# code using LuceneQuery but that does not meet my requirement.
Can someone help me resolve this issue?
If you want to search key-value only, you can do it very simple
//the index
public class FlattenIndex: AbstractIndexCreationTask<PortalEntry>
{
public class ReduceResult
{
public string Key { get; set; }
public string Value { get; set; }
}
public FlattenIndex()
{
Map = portalEntries => from portalEntry in portalEntries
from p in portalEntry.MetadataProperties.Concat(portalEntry.NamedProperties)
select new
{
Key=p.Key,
Value=p.Value
};
}
}
//the query
using (var session = _docStore.OpenSession())
{
var someEntries = session.Query<FlattenIndex.ReduceResult, FlattenIndex>()
.Where(x => x.Key == "Language" && x.Value == "English")
.As<PortalEntry>()
.ToArray();
if (someEntries!=null)
foreach(var entry in someEntries )
{
Console.WriteLine(entry.Id);
}
}
If I have this ConcurrentDictionary:
public class User
{
public string Context { get; set; }
public bool Owner { get; set; }
}
protected static ConcurrentDictionary<User, string> OnlineUsers = new ConcurrentDictionary<User, string>();
Does anyone know how I would get the value of Owner if I already have the value of the Context? Basically I want to do a "find" using the Context. Thanks.
Does anything stop you from using standard Linq FirstOrDefault() method like so:
var item = OnlineUsers.FirstOrDefault(kvp => kvp.Key.Context == myContext);
How obout something like
var ou = OnlineUsers.First(x => x.Key.Context == "TADA");
It sounds like you need a Dictionary<string, User> which, when given the context as a string will tell you which User it corresponds to. If you are going to need to perform this lookup several times, and there isn't a problem using the additional memory, it may be worth creating such a dictionary.
If you are only going to be doing the search once or twice, or the mappings will be changing so often that you can't keep the dictionary up to date, then you can simply do a linear search on the dictionary using a foreach loop, or using a LINQ method such as First or Where, as demonstrated in other answers.
Here you go:
ConcurrentDictionary<User, string> dict = new ConcurrentDictionary<User, string>();
dict.TryAdd(new User() { Context = "a", Ownder = false }, "aa");
dict.TryAdd(new User() { Context = "b", Ownder = false }, "bb");
dict.TryAdd(new User() { Context = "c", Ownder = true }, "cc");
dict.TryAdd(new User() { Context = "d", Ownder = false }, "dd");
IEnumerable<User> list = dict.Keys.Where(p => p.Context == "a");
Hi I'm kind of newbie in C#, I have this javascript code that I need to convert into corresponding c# dictionary
given below is the javascript code
EXCHANGES = {
"ICE": {
source: "SPC",
name: "ICE Futures Europe",
assetTypes: {
CER: {
spot: "ECX CER DAILY FUTURE",
fut: "ECX CER FUTURE"
},
EUA: {
spot: "ECX EUA DAILY FUTURE",
fut: "ECX EUA FUTURE"
}
}
},
"CLIMEX": {
source: "CLX",
name: "Climex Spot Market",
symbols: {
CER: {
spot: ["CER"]
},
EUA: {
spot: ["EUA 08-12"]
}
}
},
"BLUENEXT": {
source: "BLNXT",
name: "Bluenext",
symbols: {
CER: {
spot: ["1000019-82-1"]
},
EUA: {
spot: ["1000019-81-1"]
}
}
}
};
So far what I have manage is this
public Dictionary<string, Dictionary<string, string>> Exchanges = new Dictionary<string, Dictionary<string, string>> {
{"ICE ECX", new Dictionary<string, string> {
{"source", "SPC"},
{"name", "ICE Futures Europe"},
{"exchange", "ICE" }
}},
{"Climex", new Dictionary<string, string> {
{"source", "CLX"},
{"name", "Climex Spot Market"},
{"exchange", "Climex" }
}},
{"BlueNext", new Dictionary<string, string> {
{"source", "BLNXT"},
{"name", "Bluenext"},
{"exchange", "BlueNext" }
}},
{"Green Exchange", new Dictionary<string, string> {
{"source", "SPC"},
{"name", "NYMEX: Futures"},
{"exchange", "NYMEX" }
}},
{"NYMEX_FA", new Dictionary<string, string> {
{"source", "SPC"},
{"name", "NYMEX: Futures Acess"},
{"exchange", "NYMEX_FA" }
}}
};
Can anyone of you can guide me to the correct way to do this any help will be appreciated.
thanks
Pranay
I'd use a JSON Parser. Like this.
In that way you can retrieve the properties and values after parsing from the JObject.
string jsonText = #"{
prop1: 1,
prop2: 'Some String',
'prop3': {
iProp1: 1,
iProp2: 2
}";
JObject parsedJson = JObject.Parse(jsonText);
I'd give a try to ExpandoObject, which is dynamic and implements IDictionary<string,object>. For instance, you can write the following code:
dynamic x = new ElasticObject();
x.Ice = new ExpandoObject();
x.Ice.Source = "SPC";
x.Ice.Name = "Ice Futures Europe";
x.AssetTypes = new ExpandoObject();
x.AssetTypes.CER = new ExpandoObject();
x.AssetTypes.CER.Spot = "EXC CER DAILY FUTURE";
x.AssetTypes.CER.Fut = "EXC CER FUTURE";
x.EUA = new ExpandoObject();
x.EUA.Spot = "ECX EUA DAILY FUTURE";
x.EUA.Fut = "ECX EUA FUTURE";
On MSDN you can read more about ExpandoObject here.
If you need a yet smarter object, there is the ElasticObject (described here). Using it you can skip the declaration of the nested Expandos, like this:
dynamic x = new ExpandoObject();
x.Ice.Source = "SPC";
x.Ice.Name = "Ice Futures Europe";
x.AssetTypes.CER.Spot = "EXC CER DAILY FUTURE";
x.AssetTypes.CER.Fut = "EXC CER FUTURE";
x.EUA.Spot = "ECX EUA DAILY FUTURE";
x.EUA.Fut = "ECX EUA FUTURE";
Hope this helps.
This what I did basically I created classes since maintaining this level of dictionary would have been cumbersome
public class Exchange
{
public string Source {get; set;}
public string Name {get; set;}
public Dictionary<string, AssetTypeSymbol> AssetTypes {get; set;}
public Dictionary <string,AssetTypeSymbol>Symbols {get; set;}
}
public class AssetTypeSymbol
{
public IList<string> Spot {get; set;}
public IList<string> Fut {get; set;}
}
public Dictionary<string,Exchange> Exchanges = new Dictionary<string,Exchange>
{
{"ICE ECX", new Exchange() {
Source = "SPC",
Name = "ICE Futures Europe",
AssetTypes = new Dictionary<string, AssetTypeSymbol>() {
{"CER", new AssetTypeSymbol() {
Spot = new string[] { "ECX CER DAILY FUTURE" },
Fut = new string[] { "ECX CER FUTURE" }
}},
.......... and so on