Bad Performance with ExpandoObject - c#

im having a terrible problem with a List of ExpandoObject reading their data with a Incoming Products, I cant use a normal class cause im using a list of products, and some products use size as number(20,21,22...), other as letter(S, M, L, XL....) i put this on a Grid, in each size column i put a quantity that each store receives from supplier
this is my list:
listaGrade = new List<dynamic>();
foreach(product p in BD.Proc)
{
foreach(store s in BD.Stores)
{
dynamic itemGradeDinamic = new ExpandoObject();
itemGradeDinamic.MODELDES = p.MODELDES;
itemGradeDinamic.TIPO = p.TIPO;
itemGradeDinamic.DESCRIPTION = p.DESCRIPTION;
itemGradeDinamic.STORE = s.CODE;
itemGradeDinamic.STOREDES = s.NAME;
itemGradeDinamic.SUBTOTAL = 0;
foreach (string size in p.sizes)
{
((IDictionary<String, Object>)itemGradeDinamic).Add(size, 0);
}
listaGrade.add(itemGradeDinamic);
}
}
Getting data:
object description, modelDes = "";
object qtde, store = 0;
foreach (ExpandoObject obj in listaGrade)
{
foreach (string size in "products".sizes)
{
if (((IDictionary<string, object>)obj).TryGetValue(size, out qtde))
{
if (Convert.ToDecimal(qtde) > 0)
{
((IDictionary<string, object>)obj).TryGetValue("DESCRIPTION", out description);
((IDictionary<string, object>)obj).TryGetValue("MODELDES", out modelDes);
((IDictionary<string, object>)obj).TryGetValue("TIPO", out tipo);
((IDictionary<string, object>)obj).TryGetValue("SHOP", out store);
}
}
}
}

The ExpandoObject class does not replace static types.
In this particular case, you have to discard the ExpandoObject in favour of the custom static class. If you need something that contains a dynamic data structure, use Dictionary<string,string> or Dictionary<string,object> depending on the task.

Related

Accessing elements of an Array (of any element type) from an object

I use management to access devices properties and I wrote a code below to create an array of dictionaries. my application show properties in a listview control; so I need to convert all properties value to simple string
Dictionary<string,string>[] getInfo(string k) {
// using `k` as management-key
var mos = new ManagementObjectSearcher($"select * from {k}");
var devices = new List<Dictionary<string, string>>();
var mosc = mos.Get(); // mosc is a collection of all devices with same key
foreach (var device in mosc) {
var properties = new Dictionary<string, string>();
foreach (var p in device.Properties) {
if (p.Value != null) {
if (p.IsArray) {
// I have problem in here
// my application must convert p.value to string
var collection = (IEnumerable<object>)p.Value
properties[p.Name] = string.Join(", ", collection.Select(x=>x.ToString()));
} else
properties[p.Name] = p.Value.ToString();
} else properties[p.Name] = "";
}
devices.Add(properties);
}
return devices.ToArray();
}
p.Value type is object but sometimes it contain an array like UInt[] or String[], I found part of code from stackoverflow but it didn't help me and it say:
System.InvalidCastException: 'Unable to cast object of type 'System.UInt16[]' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.'
I tried code below too, but it say same thing:
int[] array = new int[] { 0, 1, 2 }; // <- I haven't access to `array` in my main problem
object obj=array;
// I only can use `obj`
// `obj` is similar to `p.Value` here
IEnumerable<object> collection = (IEnumerable<object>)obj; // <- this line throws exception!
string output=string.join(", ",collection.Select(x=>x.ToString()));
I also tried this codes:
var collection= p.Value as IEnumerable;
// ^ found this line from stackoverflow
// says: Using the generic type 'IEnumerable<T>' requires 1 type arguments
var collection= p.Value as IEnumerable<object>
// `collection` will be null
var collection= (object[]) p.Value
// says: Unable to cast object of type 'System.Int32[]' (or some something like String[]) to type 'System.Object[]'.
IEnumerable<T> is covariant in T so this would be allowed:
IEnumerable<Giraffes> giraffes = ....
var animals = (IEnumerable<Animal>)giraffes;
So then, why does this not work as well?
var array = new[] { 1, 2, 3 };
var objects = (IEnumerable<object>)array; //will not compile
int extends object, right?
Well, the reason is that type variance in C# is only allowed between reference types; the rule is that variance must preverse identity, and there is no way to cast a value type preserving identity in C#; only reference types can and those types of conversion are called reference conversions:
var animal = (Animal)giraffe;
var o = (object)"Hello";
IFish fish = cod;
//etc.
The bits that make up the object dont change, only the type of the refernece changes, hence the name of the conversion. Note that in the very first example, animals and giraffes are the same object, object.ReferenceEquals(animals, giraffes) will return true; we've only changed the type of the variable referencing it.
In your case, to obtain an IEnumerable<object> from an IEnumerable<someValueType>, you'll have to enumerate and box each item creating a new enumerable of the desired type. In order to do so, you can use the extension method Enumerable.Cast<T>():
IEnumerable<object> objects = array.Cast<object>();
Or do the projection yourself:
IEnumerable<object> objects = array.Select(i => (object)i);
second code:
int[] array = new int[] { 0, 1, 2 };
object obj=array;
var obj_array=(Array)obj;
IEnumerable<object> collection = obj_array.Cast<object>();
string output=string.join(", ",collection.Select(x=>x.ToString()));
so main code will be:
Dictionary<string,string>[] getInfo(string k) {
// using `k` as management-key
var mos = new ManagementObjectSearcher($"select * from {k}");
var devices = new List<Dictionary<string, string>>();
var mosc = mos.Get(); // mosc is a collection of all devices with same key
foreach (var device in mosc) {
var properties = new Dictionary<string, string>();
foreach (var p in device.Properties) {
if (p.Value != null) {
if (p.IsArray) {
var array = (Array)p.Value;
var collection = array.Cast<object>();
properties[p.Name] = string.Join(", ", collection.Select(x=>x.ToString()));
} else
properties[p.Name] = p.Value.ToString();
} else properties[p.Name] = "";
}
devices.Add(properties);
}
return devices.ToArray();
}

How to access properties of the object type c#?

var obj1 = new A()
{
Name = "abc",
Id = 1
}
var obj2 = new B()
{
Place = "XYZ",
Pincode = 123456
}
var obj3 = new C()
{
Mark = 100,
Standard = "Tenth"
}
var myList = new List<object>();
myList .add(obj1);
myList .add(obj2);
myList .add(obj3);
This is my code structure. I need to access the properties of the myList object. i.e) I need to access the properties like Name, Id, Place, Pincode, Mark, Standard from the myList object and it's corresponding values.
How to achieve it?
As I wrote in my comment, usually keeping completely different types in the same collection is wrong. However, that doesn't mean that's always the case, and so assuming you have a good enough reason to do that - here's one option to do it.
Assuming c# 7 or higher, your best option would probably be to (ab)use switch with pattern matching:
foreach(var obj in myList)
{
switch(obj)
{
case A a:
DoSomethingWithA(a);
break;
case B b:
DoSomethingWithB(b);
break;
}
}
You can try something like below. Working code here
public static List<List<string>> GetProperties(List<object> myList)
{
// If you don't want two lists, you can use Dictionary<key, value>
List<string> props = new List<string>();
List<string> values = new List<string>();
foreach (var a in myList)
{
if(a == null)
continue;
var propsInfo = a.GetType().GetProperties();
foreach (var prop in propsInfo)
{
if(!props.Contains(prop.Name))
{
props.Add(prop.Name);
values.Add(prop.GetValue(a, null).ToString());
}
}
}
return new List<List<string>> { props, values};
}
If you are simply looking to get values out of an object (property values), that is a different story.
foreach (objectType obj in mylist)
{
someVariable1 = obj.name;
someVariable2 = obj.Id;
}
Here is some code I use to compare two objects. I don't know if this is your scenario but I hope it helps you get what you need it for.
protected List<string> GetProperties(List<object> myList)
{
List<string> props = new List<string>();
foreach (var a in myList)
{
if(a == null)
continue;
var propsInfo = a.GetType().GetProperties();
foreach (var prop in propsInfo)
{
if(!props.Contains(prop.Name))
{
props.Add(prop.Name);
}
}
}
return props;
}

IDictionary<String, List<OpenXmlCompositeElement>> - get the List<OpenXmlCompositeElement>?

I'm working with Open XML & I have a IDictionary<String, List<OpenXmlCompositeElement>> structure. I want to work with the List part of the structure but this.map.Values tries to wrap it in an ICollection. How can I get the List part from my structure?
public List<OpenXmlCompositeElement> MapData()
{
//this does not work
return this.map.Values;
}
Since it is a dictionary, it expects you to tell from which key you want the value.
So this would be the code you need, where yourKey is the key you want to retrieve:
public List<OpenXmlCompositeElement> MapData()
{
return this.map["yourKey"];
}
If you have no interest in the key, and the dictionary is just a dictionary because the serializer says so, you could get the first item for example like this:
public List<OpenXmlCompositeElement> MapData()
{
return this.map.Values.First();
}
You can either loop through the dictionary and use the value you would like, or access the List directly using the key (in this case it's a string)
IDictionary<String, List<OpenXmlCompositeElement>> myDictionary;
List<OpenXmlCompositeElement> myList = myDictionary["myKey"];
Where myKey is available in the dictionary.
Alternatively you can loop through
foreach (var item in myDictionary)
{
var key = item.Key;
var value = item.Value
// You could then use `key` if you are unsure of what
// items are in the dictionary
}
Assuming this is your dictionary...
IDictionary<string, List<OpenXmlCompositeElement>> items = ...;
Get a specific List by key...
List<OpenXmlCompositeElement> list = items["key"];
Get the first list in the dictionary...
List<OpenXmlCompositeElement> list = items.Values.First();
Concatenate all lists in the dictionary into a single list...
List<OpenXmlCompositeElement> list = items.SelectMany(o => o).ToList();
foreach(KeyValuePair<string, List<OpenXmlCompositeElement>> kvp in IDictionary)
{
string key = kvp.key
List<OpenXmlCompositeElement> list = kvp.Value;
foreach(OpenXmlCompositeElement o in list)
{
Do anything you need to your List here
}
}
I am working with dictionaries as well, so here is a real example that I am currently working with:
foreach(KeyValuePair<string, List<DataRecords>> kvp in vSummaryResults)
{
string sKey = kvp.Key;
List<DataRecords> list = kvp.Value;
string[] vArr = sKey.Split(',');
int iTotalTradedQuant = 0;
double dAvgPrice = 0;
double dSumQuantPrice = 0;
double dQuantPrice = 0;
double dNumClose = 0;
foreach (DataRecords rec in list)
{
if(vSummaryResults.ContainsKey(sKey))
{
iTotalTradedQuant += rec.iQuantity;
dQuantPrice = rec.iQuantity * rec.dInputTradePrice;
dSumQuantPrice += dQuantPrice;
dAvgPrice = dSumQuantPrice / iTotalTradedQuant;
dNumClose = rec.dNumericClosingPrice;
}
else
{
vSummaryResults.Add(sKey, list);
//dNumClose = rec.dNumericClosingPrice;
}

How to iterate through IEnumerable collection using for each or foreach?

I want to add one by one values but in for loop how can I iterate
through one by one values and add it inside dictionary.
IEnumerable<Customer> items = new Customer[]
{
new Customer { Name = "test1", Id = 111},
new Customer { Name = "test2", Id = 222}
};
I want to add { Name = "test1", Id = 111} when i=0
and want to add { Name = "test2", Id = 222} when i=1 n so on..
Right now i'm adding full collection in every key.(want to achieve this using foreach or forloop)
public async void Set(IEnumerable collection)
{
RedisDictionary<object,IEnumerable <T>> dictionary = new RedisDictionary>(Settings, typeof(T).Name);
// Add collection to dictionary;
for (int i = 0; i < collection.Count(); i++)
{
await dictionary.Set(new[] { new KeyValuePair<object,IEnumerable <T> ( i ,collection) });
}
}
If the count is need and the IEnumerable is to be maintained, then you can try this:
int count = 0;
var enumeratedCollection = collection.GetEnumerator();
while(enumeratedCollection.MoveNext())
{
count++;
await dictionary.Set(new[] { new KeyValuePair<object,T>( count,enumeratedCollection.Current) });
}
New version
var dictionary = items.Zip(Enumerable.Range(1, int.MaxValue - 1), (o, i) => new { Index = i, Customer = (object)o });
By the way, dictionary is a bad name for some variable.
I'm done using
string propertyName = "Id";
Type type = typeof(T);
var prop = type.GetProperty(propertyName);
foreach (var item in collection)
{
await dictionary.Set(new[] { new KeyValuePair<object, T>(prop.GetValue(item, null),item) });
}
So you want to a an item from the collection to the dictionary in the for loop?
If you cast your IEnumerable to a list or an array, you can easily access it via the index. For example like this:
Edit: Code at first created a list every time it looped, which should of course be avoided.
var list = collection.ToList(); //ToArray() also possible
for (int i = 0; i < list.Count(); i++)
{
dictionary.Add(i, list[i]);
}
I'm not 100% if that is what you need, though. More details to your question would be great.

Filtering a list in c#

So i have two lists .One is an Attorney object list and the other is a list with GUID .I am first just filling the GUID list with values and then looping through it and then matching it to the ID field of Attorney object to get the Attorney's with the given ID .
Is there a nicer way than my try to achieve this.
List<Attorney> Attorneys = msg.CaseDocument.Attorneys;
List<Attorney> CaseDefendantAtt = new List<Attorney>();
List<Guid> AttorneyID = new List<Guid>();
foreach (var s in msg.CaseDocument.Defendants)
{
AttorneyID.AddRange(s.Attorneys);
}
foreach (var p in AttorneyID)
{
var z = Attorneys.FindAll(o => o.AttorneyId == p);
if (z != null)
{
CaseDefendantAtt.AddRange(z);
}
}
How about...
var caseDefendantAtt = msg.CaseDocument.Attorneys.Where(o =>
msg.CaseDocument.Defendants.SelectMany(d => d.Attorneys).Contains(o.AttorneyId));
Try this
var allAttorneyIds = msg.CaseDocument.Defendants.SelectMany(x=> x.Attroneys);
var caseDefendantsAtt = msg.CaseDocument.Attorneys.Where(x=> allAttorneyIds.Contains(x.AttorneyId)).ToList();
Maybe you could store your Attorneys in a Dictionary of [Guid, Attorney], then looking for each Guid in Dictionary.
Dictionary<Guid, Attorney> Attorneys = CreateDictionaryFrom(msg.CaseDocument.Attorneys);
List<Attorney> CaseDefendantAtt = new List<Attorney>();
List<Guid> AttorneyID = new List<Guid>();
foreach (var s in msg.CaseDocument.Defendants)
{
AttorneyID.AddRange(s.Attorneys);
}
foreach (var p in AttorneyID)
{
var z = Attorneys[p];
if (z != null)
{
CaseDefendantAtt.Add(z);
}
}
It will increase the performance with the x1000 factor. But you should notice that we assume there is only one attorney per Guid.
Without using a dictionary, we could improve your search in list seeking for FirstOrDefault instead of FindAll

Categories