How to assign values to IEnumerable<dynamic> - c#

I have the following code:
var db = Database.Open("Text");
var headers = db.Query("SELECT * FROM Headers");
//headers is now an IEnumerable<dynamic>
string s = headers[0].Text; // Works correctly
headers[0].Text = "Some string";
If I try to assign "Some string" to the Text property of the headers I get the following error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: WebMatrix.Data.DynamicRecord contains no definition for Text.
How can I convert the headers object to a List/Array so that I am able to assign new values to it?

The problem is that the DynamicRecord class being returned from WebMatrix is effectively 'read-only'. The DynamicObject does not allow setting of Text.
You would need to map this data onto your own classes if you want to make changes to the values, and not leave them within the original dynamic type.

Related

How to Update KTA RunTimeField with an object?

I am trying to update an object (Generic List object) to RuntimeFiledCollection. When I try to update using the following code, I am getting always Serialization error :(
//My object which need to update set with the documentRuntimeField value
List<docInfo> docInfoList = new List<docInfo>
docInfo = new docInfo { ID = "11233", PageNumber = 1, text ="MyOwnText"};
docInfoList.Add(docInfo);
// Construct DOcument RuntimeFields Collection
var docRunTimeCollection = new CaptureDocumentService.RuntimeFieldCollection();
var docRunTimeField = new CaptureDocumentService.RuntimeField
{ Name = "FieldName", Value = docInfoList };
docRunTimeCollection.Add(docRunTimeField);
captureDocumentServiceClient.UpdateDocumentFieldValues(sessionId, null, validDocumentId, docRunTimeCollection);
I always get sterilization error as shown below. Can someone give me an example how to update document field values with an object. Any help ?
Error : There was an error while trying to serialize parameter http://www.kofax.com/agilityservices/sdk:runtimeFields.InnerException message was System.Collections.Generic.List
For RuntimeField class, you can see the the Value property is of type Object, but that is misleading, because it is not an arbitrary container. It takes the type that you defined for that field in your Extraction Group. But essentially that just means it will take a DateTime for a Date field or numeric types for a Number field, and anything else would be casted to string.
If you actually using a Table Field, then you cannot use the API to set the contents of the entire table. You would instead set the RuntimeField.TableRow and RuntimeField.TableColumn properties to use the API to set individual cells.

How to get GridLookup with complex name

I'm trying to get GridLookup selected value,
#Html.DevExpress().GridLookup(settingsGrid =>
{
settingsGrid.Name = "CommandeDetailEnCours.Compte";
settingsGrid.KeyFieldName = "Numero";
[...]
}).BindList(Model.ListeComptes).Bind(Model.CommandeDetailEnCours.Compte).GetHtml()
The question is: How I can get Value because in javascript
"CommandeDetailEnCours.Compte".GetGridView() [...]
not work ?
Use the client-side GetValue method in the following manner.
To retrieve the complex name on the client use the How to use the ClientInstanceName and Name properties when they contain special characters approach.
var lookupClientInstance = ASPxClientControl.GetControlCollection().GetByName("CommandeDetailEnCours.Compte");
var value = lookupClientInstance.GetValue();
alert(value);

Set custom radio button field in jira through rest api c#

I'm trying to set the value of a custom radio button field that has 3 possible options: Yes, No or none. I'm using the Dapplo c# Jira SDK.
I've tried everything going from using "name" to "value" to "id" but nothing seems to work, I always get back "Could not find valid 'id' or 'value' in the Parent Option object".
Note that I want to set the field value by using the ID of the optionset value, not the textual value (like Yes or No) as that might change over time.
These are some of my attempts:
string jsonString = #"{""id"": ""11684""}";
var myJSON = JsonConvert.SerializeObject(jsonString);
issueToCreate.Fields.CustomFields.Add("customfield_12093", jsonString); //When I enter the 'jsonString' variable directly I get the same error.
does anyone have any idea? This seems much harder than it should be..
After trying about everything, the following finally worked:
var optionSetObject = new { id = "11681" };
issueToCreate.Fields.CustomFields.Add("customfield_12093", optionSetObject);
I thought the c# anonymous object wouldn't get serialized correctly, but apparently it does. Make sure the "id" value is a string though! If you enter it as an int, Jira won't recognize it for some reason.
You can try: (like use JavaScriptSerializer to serialise an anonymous object):
var optionSetObject = new { id = "11681" };
var serializer = new JavaScriptSerializer();
var output = serializer.Serialize(optionSetObject);
issueToCreate.Fields.CustomFields.Add("customfield_12093", output);

Index dynamic Objects with ElasticSearch Nest - StackOverflow Exception

I want to index a collection of dynamic objects using the ElasticSearch Nest Client.
The objects are first materialized by NewtonsoftJson.NET from a JSON file into dynamic objects and then manipulated by the program. All objects get a property "Id". This should serve as "_id" field for ElasticSearch.
The "_id" field must be the same for identical data records in order to be able to update data later.
Because attributes "IdProperty" cannot be added to a dynamic object and a
mapping can also not be used, I was forced to the following solution.
I would like to keep the dynamic objects because I manipulate only a few properties and the other properties are of no interest to me.
var values = new List<dynamic>();
dynamic obj = new System.Dynamic.ExpandoObject();
obj.Id = "ABC";
obj.SomeValue0 = "12";
obj.SomeValue1 = 99;
values.Add(obj);
var descriptor = new BulkDescriptor();
// Now i want to Index this List
foreach (var doc in values) {
// Here the StackOverflowException will be thrown
descriptor.Index<object>(i => i
.Index("abc")
.Id(doc.Id)
.Document(doc));
}
client.Bulk(descriptor);
(Index a dynamic object using NEST - This was my inspiration)
This example raises a StackOverflow exception during indexing. It does not matter whether one or more objects are in the list.
Interestingly, the following method works without problems. The only thing that doesn't work is the
ElasticSearch "_id" field was generated automatically and therefore does not correspond to the "Id" field.
client.IndexMany(value, index);
What am I doing wrong with the first possibility and is there a possibility to set a "_id" on a dynamic object?
You need to
cast the Id property to string (or Nest.Id if it could be another type with an implicit conversion to Nest.Id, for example, Guid
cast the object to object
Here's an example
var client = new ElasticClient();
var values = new List<dynamic>();
dynamic obj = new System.Dynamic.ExpandoObject();
obj.Id = "ABC";
obj.SomeValue0 = "12";
obj.SomeValue1 = 99;
values.Add(obj);
var descriptor = new BulkDescriptor();
// Now i want to Index this List
foreach (var doc in values)
{
descriptor.Index<object>(i => i
.Index("abc")
.Id((Id)doc.Id)
.Document((object)doc));
}
client.Bulk(descriptor);
which will send a request like the following
POST http://localhost:9200/_bulk
{"index":{"_index":"abc","_type":"object","_id":"ABC"}}
{"Id":"ABC","SomeValue0":"12","SomeValue1":99}
Dynamic types don't play nicely with generic types and member access expressions, which looks to be related to runtime type resolution. In the example above, I would recommend using anonymous types and a List<object>

Anonymous type dynamic object error cant implicitly convert string to int

In order to make sortable an asp grid bound to an linq result set at runtime with dynamic fields (so I can't create template fields) I decided to try serializing the table's results and putting it in a viewstate to be called, de serialized, ordered and rebound on the gridview's sorting event.
Unfortunately the assigning to a new type after the fact gives the error Cannot implicitly convert type 'string' to 'int', but I cant work out why/where it would be trying to do this.
The code up to the point of failure is:
string sSData = (string)ViewState["resultData"];
JavaScriptSerializer jSS = new JavaScriptSerializer();
dynamic dynObject = jSS.DeserializeObject(sSData);
var resultsdDS = new
{
Student_Forename = dynObject["Student_Forename"],
Student_Surname = dynObject["Student_Surname"]
};
There is the correct datatypes in the dynObject (some are ints), but not in the selected fields though the error indicates its trying to turn a string to an int, not vice versa (and these fields arent assigned anyway in this test)
Anything to help me understand what's going on would be appreciated!

Categories

Resources