Dictionary with tuples values returning null? - c#

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.

Related

Map [name,value] string values to class without reflection

I'm having a huge performance issue about mapping string property names and string property values to classes using reflection.
My issue now:
public class Person
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
public string Property4 { get; set; }
// My class has around 100 properties
public string Property100 { get; set; }
}
I am mapping a key value pair collection to the class using reflection
[{"Property1": "some value"}, {"Property2": "something else"},{"Property3","Property4","value" }.....{"Property100","val"}]
It got to the point that I am now mapping around 10 000 class instances using reflection and the performance is to say it lightly bad.
Any ideas for eliminating the reflection would be greatly appreciated.
I see two options, if you need to avoid reflection for tasks like this(when code could be programatically generated).
First is Expressions I use it often, e.g. I saw some people write something like this
public class A
{
public Prop1 ...
....
public Prop100
public override ToString() => $"{nameof(Prop1)}={Prop1};...";
and so for all 100 properties, and always doing this manually.
And with Expression it can be easily automated, you just need to generate Expression for String.Concat and pass list of properties and names there.
For your example, it is not clear what are your data. How do you do lookup in the list?
Let's assume there is a dictionary<string,string>(you can transform your list of tuples to a dictionary), and all properties are strings as well.
Then we would need to generate a list assignment expressions like this
if(data.ContainsKey("Prop1")) result.Prop1 = data["Prop1"];
And the code would be complicated, anyway it would look like this
private static class CompiledDelegate<T>
{
public static Action<T, Dictionary<string, string>> initObject;
static CompiledDelegate()
{
var i = Expression.Parameter(typeof(Dictionary<string, string>), "i");
var v = Expression.Parameter(typeof(T), "v");
var propertyInfos = typeof(T).GetProperties().ToArray();
var t = new Dictionary<string, string>();
var contains = typeof(Dictionary<string, string>).GetMethod(nameof(Dictionary<string, string>.ContainsKey));
var getter = typeof(Dictionary<string, string>).GetProperties().First(x => x.GetIndexParameters().Length > 0);
var result = new List<Expression>();
foreach (var propertyInfo in propertyInfos)
{
var cst = Expression.Constant(propertyInfo.Name);
var assignExpression =
Expression.IfThen(Expression.Call(i, contains, cst),
Expression.Assign(Expression.PropertyOrField(v, propertyInfo.Name), Expression.MakeIndex(i, getter, new[] { cst })));
result.Add(assignExpression);
}
var block = Expression.Block(result);
initObject = Expression.Lambda<Action<T, Dictionary<string, string>>>(block, new ParameterExpression[] { v, i }).Compile();
}
}
It is an example, it would fail if there were non-string properties.
And it could be used like this
static void Main(string[] args)
{
var tst = new Test();
CompiledDelegate<Test>.initObject(tst, new Dictionary<string, string>
{
{ "S3", "Value3" },
{ "S2", "Value2" },
});
CompiledDelegate<Test>.initObject(tst, new Dictionary<string, string>
{
{ "S3", "Value3" },
{ "S1", "Value1" },
});
Console.ReadKey();
}
The second option is, actually, what it should be ideally imlemented like Using source generators I think such things do have to be done just in build time.
There is a lot of articles on msdn, for instance with samples. But it turned out to be not very easy to implement, even just a sample.
I can say, it didn't work for me, while I tried to do it according to samples.
In order to get it work I had to change TargetFramework to netstandard2.0, do something else...
But after all, when build was green, Visual Studio still showed an error.
Ok, it disappeared after VS restart, but still, that doesn't look very usable.
So, this is a generator, that creates a converter for every class with attribute.
It is again a sample, it doesn't check many things.
[Generator]
public class ConverterGenerator : ISourceGenerator
{
private static string mytemplate = #"using System.Collections.Generic;
using {2};
namespace GeneratedConverters
{{
public static class {0}Converter
{{
public static {0} Convert(Dictionary<string, string> data)
{{
var result = new {0}();
{1}
return result;
}}
}}
}}";
public static string GetNamespaceFrom(SyntaxNode s)
{
if (s.Parent is NamespaceDeclarationSyntax namespaceDeclarationSyntax)
{
return namespaceDeclarationSyntax.Name.ToString();
}
if (s.Parent == null)
return "";
return GetNamespaceFrom(s.Parent);
}
public void Execute(GeneratorExecutionContext context)
{
GetMenuComponents(context, context.Compilation);
}
private static void GetMenuComponents(GeneratorExecutionContext context, Compilation compilation)
{
var allNodes = compilation.SyntaxTrees.SelectMany(s => s.GetRoot().DescendantNodes());
var allClasses = allNodes.Where(d => d.IsKind(SyntaxKind.ClassDeclaration)).OfType<ClassDeclarationSyntax>();
var classes = allClasses
.Where(c => c.AttributeLists.SelectMany(a => a.Attributes).Select(a => a.Name).Any(s => s.ToString().Contains("DictionaryConverter")))
.ToImmutableArray();
foreach (var item in classes.Distinct().Take(1))
{
context.AddSource(item.Identifier.Text + "Converter", String.Format(mytemplate, item.Identifier.Text, SourceText.From(GenerateProperties(item)), GetNamespaceFrom(item)));
}
}
private static string GenerateProperties(ClassDeclarationSyntax s)
{
var properties = s.Members.OfType<PropertyDeclarationSyntax>();
return String.Join(Environment.NewLine,
properties.Select(p =>
{
var name = p.Identifier.Text;
return $"if(data.ContainsKey(\"{name}\")) result.{name} = data[\"{name}\"];";
}));
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
and
static void Main(string[] args)
{
var t1 = GeneratedConverters.TestConverter.Convert(new Dictionary<string, string>
{
{ "S3", "Value3" },
{ "S2", "Value2" },
});
}
Best performance without reflection would be manual mapping.
It seems your key/value pair collection is regular JSON. So you could use the JSONTextReader from JSON.NET and read the string. Then manually map the JSON properties to the class properties.
Like so:
JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));
while (reader.Read())
{
if (reader.Value != null)
{
// check reader.Value.ToString() and assign to correct class property
}
}
More info can be found on the JSON.NET website : https://www.newtonsoft.com/json/help/html/ReadingWritingJSON.htm

How to deserialize this JSON to C# Class

i'am working with a WebAPI that returns this json from a Request
{
"apps": {
"570": {
"228983": {
"8124929965194586177": "available"
},
"228990": {
"1829726630299308803": "available"
},
"373301": {
"840315559245085162": "available"
},
"373302": {
"688854584180787739": "available"
},
"373303": {
"3675525977143063913": "available"
},
"373305": {
"4435851250675935801": "available"
},
"381451": {
"6984541794104259526": "available"
},
"381452": {
"1442783997179322635": "available"
},
"381453": {
"6878143993063907778": "available"
},
"381454": {
"7824447308675043012": "available"
},
"381455": {
"5681120743357195246": "available"
}
},
"674940": {
"674941": {
"6246860772952658709": "available"
}
}
}
}
It returns A list of AppID (int), that contains another list of DepotID, that it Contains a ManifestID (Key) and if it's aviable or not (Value).
And i want to deserialize to a class to easy work with it, but i can't imagine how to do it. I'am a newbie in C# comming from C/C++
You can use Json.NET which is a popular JSON library for C#. See Deserialize an Object.
Example:
public class MyData
{
public Dictionary<long, Dictionary<long, Dictionary<long, string>>> apps { get; set; }
}
var data = JsonConvert.DeserializeObject<MyData>(json);
// Use 'data.apps'
I'm not sure how much is gained by modeling this as C# classes without property names beyond the "apps" level of your Json, but you could do it like so:
Model your Json with the following classes:
public class AppIds : Dictionary<string, DepotId> { }
public class DepotId : Dictionary<string, ManifestId> { }
public class ManifestId : Dictionary<string, string> { }
And then you can do like so using Newtonsoft.Json
class Program
{
static void Main(string[] args)
{
string jsonPath = #"c:\debug\data.json";
System.IO.Stream s = new System.IO.FileStream(jsonPath,System.IO.FileMode.Open, System.IO.FileAccess.Read);
AppIds data = JsonConvert.DeserializeObject<Dictionary<string, AppIds>>(File.ReadAllText(jsonPath))["apps"];
}
}
You can use the Newtonsoft.Json NuGet package and deserialize your data to a nested dictionary like so:
var data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>>>(File.ReadAllText("Data.json"));
To make sure you got the right data you can run this code to print it out:
foreach (var a in data)
{
Console.WriteLine(a.Key);
foreach (var b in a.Value)
{
Console.WriteLine("\t" + b.Key);
foreach (var c in b.Value)
{
Console.WriteLine("\t\t" + c.Key);
foreach (var d in c.Value)
{
Console.WriteLine("\t\t\t" + d.Key + ": " + d.Value);
}
}
}
}
Not really sure how to deserialize this data to a class since it doesn't have much in the way of property names...

Method inside an array in C#

I just got in contact with C# and I was wondering if it's possible to call a method inside an array. I have to say that I'm working with NoSQL database (mongodb).
This is mi code, and I want to call data() method inside that JSON.
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("collection");
var document = new BsonDocument
{
{ "date", 10/04/2018 },
{ "data", data() }
};
collection.InsertOneAsync(document);
Console.Read();
}
static void data()
{
for (int i = 1; i <= 50; i++)
{
var data = new BsonDocument
{
{ "magnitude"+i, new BsonDocument{
{ "value", 5 }
} }
};
}
}
EDIT: Basically, what I'm trying to create with C# is this json below. I already did it with PHP and now, I'm trying to do it with C#.
{
"_id" : ObjectId("5abb735eb57dce214009035a"),
"date" : 1262300400,
"data" : {
"magnitude1" : {
"value" : 60
},
"magnitude2" : {
"value" : 38
},
"magnitude3" : {
"value" : 200
},
"magnitude4" : {
"value" : 62
},
"magnitude5" : {
"value" : 153
},
"magnitude6" : {
"value" : 176
},
"magnitude7" : {
"value" : 185
},
"magnitude8" : {
"value" : 168
},
.
.
.
You can use methods to gather data but I'm not sure exactly how you're asking it. Related to the code example I'll just give a simple run down, which is basic programming in general, not just C#.
You can write methods that return void or that return a variable of some type (at a minimum).
//Returns void
public void DoSomething()
{
//Do some work
return;
}
//Returns int
public int GetSomething()
{
int result = 100;
return result;
}
When you have methods that return data you can use them as you would a variable; just remember the method will execute every time it's called so it's often best to save the data to a variable. But for your example you can do something like this.
//other code ommitted
var document = new BsonDocument
{
{ "date", 10/04/2018 },
{ "data", getDocuments() }
};
//remaining code omitted
static List<BsonDocument> getDocuments()
{
var documents = new List<BsonDocument>();
for (int i = 1; i <= 50; i++)
{
var document = new BsonDocument
{
{ "magnitude" + i, new BsonDocument { { "value", 5 } } }
};
documents.Add(document);
}
return documents;
}
Now I modified the data() method to return a list of documents and changed the naming to match it but I'm not sure what you wanted to do with the method. That was my best assumption of what you were trying to accomplish by looking at your code so feel free to ignore all of it if it's wrong.
I've could solve it thanks to #Michael .Code below in case helps to anyone.
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("Collection");
var document = new BsonDocument
{
{ "date", 10/04/2018 },
{ "data", new BsonDocument{ getDocuments() } }
};
collection.InsertOneAsync(document);
Console.Read();
}
static BsonDocument getDocuments()
{
var documents = new BsonDocument();
for (int i = 1; i <= 5; i++)
{
var document = new BsonDocument
{
{ "magnitude" + i, new BsonDocument { { "value", 5 } } }
};
documents.AddRange(document);
}
return documents;
}

RavenDB Index: Need a solution to merge 2 dictionary fields into a single dictionary, flatten it and make it searchable

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);
}
}

Proper way to initialize a C# dictionary with values

I am creating a dictionary in a C# file with the following code:
private readonly Dictionary<string, XlFileFormat> FILE_TYPE_DICT
= new Dictionary<string, XlFileFormat>
{
{"csv", XlFileFormat.xlCSV},
{"html", XlFileFormat.xlHtml}
};
There is a red line under new with the error:
Feature 'collection initilializer' cannot be used because it is not part of the ISO-2 C# language specification
What is going on here?
I am using .NET version 2.
I can't reproduce this issue in a simple .NET 4.0 console application:
static class Program
{
static void Main(string[] args)
{
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
Console.ReadKey();
}
}
Can you try to reproduce it in a simple Console application and go from there? It seems likely that you're targeting .NET 2.0 (which doesn't support it) or client profile framework, rather than a version of .NET that supports initialization syntax.
With C# 6.0, you can create a dictionary in the following way:
var dict = new Dictionary<string, int>
{
["one"] = 1,
["two"] = 2,
["three"] = 3
};
It even works with custom types.
You can initialize a Dictionary (and other collections) inline. Each member is contained with braces:
Dictionary<int, StudentName> students = new Dictionary<int, StudentName>
{
{ 111, new StudentName { FirstName = "Sachin", LastName = "Karnik", ID = 211 } },
{ 112, new StudentName { FirstName = "Dina", LastName = "Salimzianova", ID = 317 } },
{ 113, new StudentName { FirstName = "Andy", LastName = "Ruth", ID = 198 } }
};
See How to initialize a dictionary with a collection initializer (C# Programming Guide) for details.
Suppose we have a dictionary like this:
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "Mohan");
dict.Add(2, "Kishor");
dict.Add(3, "Pankaj");
dict.Add(4, "Jeetu");
We can initialize it as follows.
Dictionary<int, string> dict = new Dictionary<int, string>
{
{ 1, "Mohan" },
{ 2, "Kishor" },
{ 3, "Pankaj" },
{ 4, "Jeetu" }
};
Object initializers were introduced in C# 3.0. Check which framework version you are targeting.
Overview of C# 3.0
Note that C# 9 allows Target-typed new expressions so if your variable or a class member is not abstract class or interface type duplication can be avoided:
private readonly Dictionary<string, XlFileFormat> FILE_TYPE_DICT = new ()
{
{ "csv", XlFileFormat.xlCSV },
{ "html", XlFileFormat.xlHtml }
};
With С# 6.0
var myDict = new Dictionary<string, string>
{
["Key1"] = "Value1",
["Key2"] = "Value2"
};
Here is an example of Dictionary with Dictionary value
Dictionary<string, Dictionary<int, string>> result = new() {
["success"] = new() {{1, "ok"} , { 2, "ok" } },
["fail"] = new() {{ 3, "some error" }, { 4, "some error 2" } },
};
which is equivalent to this in JSON :
{
"success": {
"1": "ok",
"2": "ok"
},
"fail": {
"3": "some error",
"4": "some error 4"
}
}
The code looks fine. Just try to change the .NET framework to v2.0 or later.

Categories

Resources