Initialize array with a mix of with hard-coded and generated values - c#

This code which initializes an array with two hard-coded values is working perfectly fine:
var db = new GoogleGraph {
cols = new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }
}.ToList(),
rows = new List<DataPointSet>()
};
db.cols.AddRange(listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" }));
This code which attempts to add some dynamically generated values is not working:
var db = new GoogleGraph {
cols = new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" },
listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" })
}.ToList(),
rows = new List<DataPointSet>()
};
How can I correctly implement the above snippet?

You can't pass an IEnumerable<T> to an initializer of T[] like that.
You can do what you want by putting the hard-coded objects in their own collection, then concatenating the dynamic ones:
var db = new GoogleGraph {
cols =
new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }
}
.Concat(listOfValues.Select(p =>
new ColInfo { id = "", label = p, type = "number" }))
.ToList(),
rows = new List<DataPointSet>()
};

Related

ExcelDataReader : I have 5 columns, the code creates one more "Column6": null . Any idea on how to avoid this or filter it?

I am using the following code-
private static HashSet<string> keepHeaders = new HashSet<string>{
"Screen", "Section", "Question Name", "Question Label",
"Question Type", "Required?", "Choice Group",
"Min", "Max", "Max Length", "Label", "Choice Label",
"Name", "Description"
};
private static string ExtractDataCatalogAlt(IFormFile file)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var excelStream = file.OpenReadStream())
using (var reader = ExcelReaderFactory.CreateOpenXmlReader(excelStream, new ExcelReaderConfiguration { FallbackEncoding = Encoding.GetEncoding(1252) }))
{
var headers = new List<string>();
var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration
{
UseHeaderRow = true,
ReadHeaderRow = (r) =>
{
// store the header row values so we can trim columns out that don't have headers
for (var i = 0; i < r.FieldCount; i++)
headers.Add(r.GetString(i));
},
FilterColumn = (_, c) =>
{
return keepHeaders.Contains(headers[c]);
}
}
});
return JsonConvert.SerializeObject(dataSet, Formatting.Indented);
}
}
Can you tell what condition should I put in FilterColumn so that it reads only cells which has values and not give output as "column6" : null

Create dynamic object from a JSON disregarding null fields

I need to consume a method that the input parameter is a dynamic object, but I feed the object through a received JSON
Dynamic Builder:
public static dynamic PayChargeObject()
{
var body = new
{
payment = new
{
banking_billet = new
{
customer = new
{
name = "",
email = "",
cpf = "",
birth = "",
phone_number = "",
address = new
{
street = "",
number = "",
neighborhood = "",
zipcode = "",
city = "",
complement = "",
state = "",
},
juridical_person = new
{
corporate_name = "",
cnpj = "",
}
},
expire_at = "",
discount = new
{
type = "",
value = 0
},
conditional_discount = new
{
type = "",
value = 0,
until_date = ""
},
message = ""
}
}
};
return body;
}
My JSON Data:
{
"payment": {
"banking_billet": {
"customer": {
"name": "person name",
"email": "personnamez#gerencianet.com.br",
"cpf": "94271564656",
"birth": "1977-01-15",
"phone_number": "41991234567"
},
"expire_at": "2019-12-12"
}
}
Note that json will not always have all available fields in the object filled, but the method I need to consume does not accept null values in the fields, and my problem is this, when deserializing JSON in the dynamic object, the fields not used in JSON are created as null in the dynamic object
JSON Convert and Method call:
var obj = JsonConvert.DeserializeAnonymousType(MyJSON, PayChargeObject())
dynamicMethod.PayCharge(obj);
Dynamic Object with null fields on Debug
How can I solve this problem?
With help of #dbc solution:
replace:
var obj = JsonConvert.DeserializeAnonymousType(MyJSON, PayChargeObject())
dynamicMethod.PayCharge(obj);
with:
dynamicMethod.PayCharge(JObject.Parse(MyJSON));

can adaptive cards in bot framework v3 contain dynamic dropdowns

I'm using BOT Framework v 3 i have an adaptive card that takes input from the user and i want the values in Dropdown to be dynamic is it possible.here is the adaptive card design code as you can see I have entered the choices manually instead it want it to be dynamic from the database
var card = new AdaptiveCard()
{
Body = new List<CardElement>()
{
new TextBlock()
{
Color = TextColor.Attention,
Weight = TextWeight.Bolder,
Size = TextSize.Medium,
Text = "Select a title",
},
new ChoiceSet()
{
Id = "title",
Style = ChoiceInputStyle.Compact,
IsRequired = false,
IsMultiSelect = false,
Value = "1",
Choices = new List<Choice>()
{
new Choice()
{
Title = "Swiss cargo",
Value = "Swiss cargo",
},
new Choice()
{
Title = "ticket booking",
Value = "ticket booking",
},
},
},
},
};
Assuming you can get your data into a list of strings, your Adaptive Card can easily be constructed dynamically using Linq. If you want to keep using the same Adaptive Cards library, it would look like this:
var data = new List<string> { "Swiss cargo", "ticket booking" };
var card = new AdaptiveCard()
{
Body = new List<CardElement>()
{
new TextBlock()
{
Color = TextColor.Attention,
Weight = TextWeight.Bolder,
Size = TextSize.Medium,
Text = "Select a title",
},
new ChoiceSet()
{
Id = "title",
Style = ChoiceInputStyle.Compact,
IsRequired = false,
IsMultiSelect = false,
Value = "1",
Choices = data.Select(item => new Choice { Title = item, Value = item }).ToList(),
},
},
};

NetSuite Web API - How to return/populate customFieldList array for Sales Order from a joined search?

I am currently performing a SuiteTalk search via C# that joins multiple tables, one of which is for Sales Orders. When performing a typical GET on a SalesOrder record, the property customFieldList gets populated with an array of transaction custom fields/etc. I am curious how to get the same when doing a search like:
SearchResult searchResult = Client.Service.search(new TransactionSearchAdvanced()
{
criteria = new TransactionSearch()
{
basic = new TransactionSearchBasic()
{
type = new SearchEnumMultiSelectField()
{
#operator = SearchEnumMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new String[] { "_salesOrder" },
},
lastModifiedDate = new SearchDateField()
{
#operator = SearchDateFieldOperator.after,
operatorSpecified = true,
searchValue = fromLastModifiedDateTime.ToUniversalTime(),
searchValueSpecified = true
}
},
},
columns = new TransactionSearchRow()
{
basic = new TransactionSearchRowBasic()
{
internalId = new SearchColumnSelectField[] { new SearchColumnSelectField() },
tranId = new SearchColumnStringField[] { new SearchColumnStringField() },
tranDate = new SearchColumnDateField[] { new SearchColumnDateField() },
dateCreated = new SearchColumnDateField[] { new SearchColumnDateField() },
item = new SearchColumnSelectField[] { new SearchColumnSelectField() },
quantity = new SearchColumnDoubleField[] { new SearchColumnDoubleField() },
lastModifiedDate = new SearchColumnDateField[] { new SearchColumnDateField() },
email = new SearchColumnStringField[] { new SearchColumnStringField() },
//customFieldList = new SearchColumnCustomField[] { },
},
itemJoin = new ItemSearchRowBasic()
{
itemId = new SearchColumnStringField[] { new SearchColumnStringField() },
type = new SearchColumnEnumSelectField[] { new SearchColumnEnumSelectField() },
},
customerJoin = new CustomerSearchRowBasic()
{
internalId = new SearchColumnSelectField[] { new SearchColumnSelectField() },
billAddress = new SearchColumnStringField[] { new SearchColumnStringField() },
companyName = new SearchColumnStringField[] { new SearchColumnStringField() },
phone = new SearchColumnStringField[] { new SearchColumnStringField() },
email = new SearchColumnStringField[] { new SearchColumnStringField() },
},
customSearchJoin = new CustomSearchRowBasic[]
{
},
}
});
The property I want populated is commented out within the TransactionSearchRowBasic object:
//customFieldList = new SearchColumnCustomField[] { },
Any ideas? Thank you in advance!
The search operation doesn't return as much information as a GET operation does on the SuiteTalk Web Services.
For each record that is returned in your SearchResult, use the internalId or document number to GET that record. This should then include your custom fields.
NetSuiteService _service = new NetSuiteService();
ReadResponse res = _service.get(new RecordRef { internalId = internalID, type = RecordType.salesOrder, typeSpecified = true });

MVC 4 DateTime? on model removal on edit

I have model which as a nullable Date field. Everything works as expected except in the circumstance where I am editing the model and I want to remove the date. Then validation is triggered:
How do I allow removal while also keeping validation on a partially filled in date? Is worth mentioning that are using a custom EditorTemplate:
Here is the code for the editor template:
#model DateTime?
#{
var months = new[] {
new { Value = "", Display="" },
new { Value = "1", Display = "01-Jan" },
new { Value = "2", Display = "02-Feb" },
new { Value = "3", Display = "03-Mar" },
new { Value = "4", Display = "04-Apr" },
new { Value = "5", Display = "05-May" },
new { Value = "6", Display = "06-Jun" },
new { Value = "7", Display = "07-Jul" },
new { Value = "8", Display = "08-Aug" },
new { Value = "9", Display = "09-Sep" },
new { Value = "10", Display = "10-Oct" },
new { Value = "11", Display = "11-Nov" },
new { Value = "12", Display = "12-Dec" }
};
var monthSelect = new SelectList(months, "Value", "Display", Model.HasValue ? Model.Value.Month.ToString() : "");
var days = new List<String>();
days.Add("");
days.AddRange(Enumerable.Range(1, 31).Select(s => s.ToString()));
var daySelect = new SelectList(days.Select(x => new KeyValuePair<string, string>(x, x)), "Key", "Value", Model.HasValue ? Model.Value.Day.ToString() : "");
}
#Html.DropDownList("Month", monthSelect)
#Html.DropDownList("Day", daySelect)
#Html.TextBox("Year", (Model.HasValue ? Model.Value.ToString("yyyy") : string.Empty), new { style="width:75px;" })
The code on the edit view is dead simple:
#Html.EditorFor(model => model.MyDate)

Categories