I have an api endpoint that returns these settings to a client HTML/JS that retrieve these data through either normal XML request or promise or async/await.
return new JsonResult(
new List<object>()
{
new { id = 1, size = "big", order = 6},
new { id = 2, size = "small", order = 4},
new { id = 3, size = "medium", order = 2},
new { id = 4, size = "small", order = 5},
new { id = 5, size = "small", order = 8, chips= new { }},
new { id = 6, size = "small", order = 7},
new { id = 7, size = "big", order = 1, chips= new { }},
new { id = 8, size = "small", order = 3},
new { id = 10, size = "big", order = 9},
new { id = 20, size = "big", order = 10}
});
however a new requirement come up that needs me to save these settings into a class and their value into a config file so we do not need to rebuild every time new settings are added.
This is the class I made:
public class Settings
{
public int Id { get;}
public string Size { get;}
public int Order { get;}
public int[] Arrays { get;}
}
I have been looking at creating configuration file and calling them. I just cannot wrap my head around how to save these value into a configuration file in an api so that it can be call in a client like this:
const getDataByPromise = function (method, url) {
return new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = function () {
if (successfulHttpResponse(this.status)) {
resolve(xhr.response);
document.getElementById("promise").textContent = xhr.responseText;
} else {
reject({
status: this.status,
statusText: xhr.statusText,
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText,
});
};
xhr.send();
});
};
How do you think I should go about implementing this
Related
Model:
{
_id: "1025",
CandidateName: "John"
Activities:["Programming", "Gaming"]
}
ChoicheOfActivities = ["Programming", "Gaming", "Singing", "Acting"];
Candidate1 = {_id:"1", Name: "John", Activities: ["Programming", "Gaming"]};
Candidate2 = {_id:"2", Name: "Mike", Activities: ["Programming", "Singing"]};
Candidate3 = {_id:"3", Name: "Joey", Activities: ["Gaming","Programming", "Singing", "Acting"]};
Candidate4 = {_id:"4", Name: "Ross", Activities: ["Programming", "Kayaking"]};
I want to filter the candidates who have activities that are sub or equal set to the ChoiceOfAtivities.
After applying the filter I want the results to be
Candidate1 = {_id:"1", Name: "John", Activities: ["Programming", "Gaming"]};
Candidate2 = {_id:"2", Name: "Mike", Activities: ["Programming", "Singing"]};
Candidate3 = {_id:"3", Name: "Joey", Activities: ["Gaming","Programming", "Singing", "Acting"]};
Candidate3 won't be selected because it has an activity "Kayaking" which is not in the ChoiceOfAtivities.
Note: I want to apply this when filtering not on a list or an Enumerable.
#varman query is correct, however, this is how we'd express it as C#/.NET
We'd start by creating a class that represents our document:
class Candidate
{
public string Id { get; set; }
public string Name { get; set; }
public string[] Activities { get; set; }
}
Then to get things started we'll just connect to mongo and insert your sample data in to the test database.
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<Candidate>("candidates");
var candidate1 = new Candidate { Id = "1", Name = "John", Activities = new[] { "Programming", "Gaming" } };
var candidate2 = new Candidate { Id = "2", Name = "Mike", Activities = new[] { "Programming", "Singing" } };
var candidate3 = new Candidate { Id = "3", Name = "Joey", Activities = new[] { "Gaming", "Programming", "Singing", "Acting" } };
var candidate4 = new Candidate { Id = "4", Name = "Ross", Activities = new[] { "Programming", "Kayaking" } };
await collection.InsertManyAsync(new[]
{
candidate1,
candidate2,
candidate3,
candidate4
});
Now we'll build up some filters using the filter builder.
var choiceOfActivities = new[] {"Programming", "Gaming", "Singing", "Acting"};
var filter = Builders<Candidate>.Filter.Not(
Builders<Candidate>.Filter.ElemMatch(candidate => candidate.Activities,
new BsonDocument("$nin", BsonArray.Create(choiceOfActivities))));
Then we can execute a find on our collection and get our the result
var results = await collection.Find(filter)
.ToListAsync();
foreach (var result in results)
{
Console.WriteLine($"{result.Id}, {result.Name}, {string.Join(" + ", result.Activities)}");
}
// 1, John, Programming + Gaming
// 2, Mike, Programming + Singing
// 3, Joey, Gaming + Programming + Singing + Acting
You can do like this way
db.collection.find({
Activities: {
$not: {
"$elemMatch": {
$nin: [
"Programming",
"Gaming",
"Singing",
"Acting"
]
}
}
}
})
Working Mongo playground
var ipSets = new CfnIPSet(scope, "IPSet", new CfnIPSetProps
{
Name = "IPTest",
Addresses = new string[] { "1.2.3.4/32" },
IpAddressVersion = "IPV4",
Scope= "REGIONAL"
});
new CfnWebACL.RuleProperty()
{
Name = "Black-List-Rules",
Priority = 5,
//statement not properly translated
Statement = new CfnWebACL.StatementOneProperty
{
IpSetReferenceStatement = ipSets.AttrArn
},
VisibilityConfig = new CfnWebACL.VisibilityConfigProperty
{
SampledRequestsEnabled = true,
CloudWatchMetricsEnabled = true,
MetricName = "Black-List-Rules"
},
Action = new CfnWebACL.RuleActionProperty
{
Allow = new CfnWebACL.RuleActionProperty { Allow = false}
},
}
TestWebACL Error reason: Your statement has multiple values set for a field that requires exactly one value., field: STATEMENT, parameter: Statement (Service: Wafv2, Status Code: 400, Request ID: dd0d6492-5aa9-41e2-ac15-ee7bc133d705, Extended Request ID: null)
C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7922:49
_ Kernel._wrapSandboxCode (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:8395:20)
_ Kernel._create (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7922:26)
_ Kernel.create (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7666:21)
_ KernelHost.processRequest (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7446:28)
_ KernelHost.run (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7384:14)
_ Immediate._onImmediate (C:\Users\futechz\AppData\Local\Temp\1hae5afq.wun\jsii-runtime.js:7387:37)
_ processImmediate (internal/timers.js:456:21)
cloud formation (cdk synth)
{
"Action": {
"Block": {
"block": true
}
},
"Name": "Black-List-Rules",
"Priority": 5,
"Statement": {}, //missing
"VisibilityConfig": {
"CloudWatchMetricsEnabled": true,
"MetricName": "Black-List-Rules",
"SampledRequestsEnabled": true
}
}
Solved
["Statement"] = new Dictionary<string, object>
{
["OrStatement"] = new Dictionary<string, object>
{
["Statements"] = new [] {
new Dictionary<string, object>
{
["IpSetReferenceStatement"] = new Dictionary<string , object> {
["Arn"] = ipSetsOne.AttrArn
},
},
new Dictionary<string, object>
{
["IpSetReferenceStatement"] = new Dictionary<string , object> {
["Arn"] = ipSetsTwo.AttrArn
},
}
}
}
},
https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html
Initializing four items into a List as follows, BUT it only initializes the first two items ... I really can't see what is wrong with this?
public List<SceneStore> lcRecordList = new List<SceneStore>
{
new SceneStore { description ="Jill ", ID = 1, visited = false },
new SceneStore { description = "Jack", ID = 2, visited = false},
new SceneStore { description = "Joe", ID = 3, visited = false},
new SceneStore { description = "Jenny", ID = 4, visited = false}
};
void NetTest()
{
NetworkService lcMyNetworkService = new NetworkService();
lcMyNetworkService.PutJsonList <SceneStore>(lcRecordList, "https://NewSimland.com/~todd/JSON", ReceiveAListOfRecords);
}
Took a screenshot
This adds four items just fine:
public List<SceneStore> lcRecordList;
void NetTest()
{
NetworkService lcMyNetworkService = new NetworkService();
lcRecordList = new List<SceneStore>
{
new SceneStore { description ="Jill ", ID = 1, visited = false },
new SceneStore { description = "Jack", ID = 2, visited = false},
new SceneStore { description = "Joe", ID = 3, visited = false},
new SceneStore { description = "Jenny", ID = 4, visited = false}
};
lcMyNetworkService.PutJsonList <SceneStore>(lcRecordList, "https://NewSimland.com/~todd/JSON", ReceiveAListOfRecords);
}
So why is initialization of lcRecordList on declaration limited to the first two?
Took another screenshot of the local variable value:
As indicated by CaTS and Mo Narimani, the UNITY3D environment is initializing the values based on the first time the list was initialized because it found a Public class attribute (aka variable) in the MonoBehaviour. That was overriding the initialization in the script after more items were added in the script code.
SO the Answer is to "refresh" that by clicking on Reset in the Inspector after adding more items , when changing the initialization on declaration, that works!!
See screenshot here:
Unity3D sticks with the first initialization?
try this instead:
public List<SceneStore> lcRecordList = new List<SceneStore>
{
SceneStore store;
store= new SceneStore { description ="Jill ", ID = 1, visited = false },
lcRecordLisr.Add(store);
store= new SceneStore { description = "Jack", ID = 2, visited = false},
lcRecordLisr.Add(store);
store= new SceneStore { description = "Joe", ID = 3, visited = false},
lcRecordLisr.Add(store);
store= new SceneStore { description = "Jenny", ID = 4, visited = false}
lcRecordLisr.Add(store);
};
I have a JSON array and I am adding items. I want to display this JSON in a particular format.
My code:
var array = new List<object>();
array.Add(new
{
Dealname = dealname,
Ticketcount = tictnum,
OriginalPrice = origpri,
Dealsticketcount = dealsticktnu,
dealprice = dp,
totalprice = totamnt,
});
array.Add(new
{
ItemName = itnme,
Price = price,
Quantity = quant,
});
This is what my array looks like. I am adding some items. Right now it produces the following output:
[{"Dealname":"unnideal","Ticketcount":"25","OriginalPrice":"100","Dealsticketcount":"1","dealprice":"200","totalprice":"300},{"ItemName":"popcorn","Price":"100","Quantity":"1"},{"ItemName":"piza","Price":"100","Quantity":"1"}]
But i need my output like this:
[{"Dealname":"unnideal","Ticketcount":"25","OriginalPrice":"100","Dealsticketcount":"1","dealprice":"200","totalprice":"300"},"Offers"[{"ItemName":"popcorn","Price":"100","Quantity":"1"},{"ItemName":"piza","Price":"100","Quantity":"1"}]]
That is, I need an array for offers. How can I make this possible?
Your problem appears to be that your parent object, and child "offer" objects are not related, when Offers needs to be part of the main object.
Try something like this:
var array = new List<object>();
var offers = new List<object>();
offers.Add(new
{
ItemName = itnme,
Price = price,
Quantity = quant,
});
array.Add(new
{
Dealname = dealname,
Ticketcount = tictnum,
OriginalPrice = origpri,
Dealsticketcount = dealsticktnu,
dealprice = dp,
totalprice = totamnt,
Offers = offers
});
Sounds like you just want another property named "Offers"?
var array = new List<object>();
var offers = new[]
{
new {ItemName = itnme, Price = price, Quantity = quant}
...
};
array.Add(new
{
Dealname = dealname,
Ticketcount = tictnum,
OriginalPrice = origpri,
Dealsticketcount = dealsticktnu,
dealprice = dp,
totalprice = totamnt,
Offers = offers // adding Offers as a property here
});
This should produce a JSON like the following:
[
{
"Dealname": "unnideal",
"Ticketcount": "25",
"OriginalPrice": "100",
"Dealsticketcount": "1",
"dealprice": "200",
"totalprice": "300",
"Offers": [
{
"ItemName": "popcorn",
"Price": "100",
"Quantity": "1"
},
{
"ItemName": "piza",
"Price": "100",
"Quantity": "1"
}
]
}
]
This is My Type:
public class MyObj {
public long Number { get; set; }
public string Name { get; set; }
public string Message { get; set; }
public DateTime RegisterDate { get; set; }
}
So I have a list of Myobj And I need to split the list to some lists where the objects of Name and Number of objects are equal, some thing like grouped by Name and Number:
assume this Sample:
List<MyObj> MyObjects = new List<MyObj>{
new MyObj() { Number = 1, Name = "BMW", Message = "msg1", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "BMW", Message = "msg2", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "BMW", Message = "msg3", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "Honda", Message = "msg11", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "Honda", Message = "msg12", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "BMW", Message = "msg22", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "BMW", Message = "msg23", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "BMW", Message = "msg24", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "Honda", Message = "msg30", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "BMW", Message = "msg41", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "BMW", Message = "msg42", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "Ford", Message = "msg51", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "Ford", Message = "msg52", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "Ford", Message = "msg53", RegisterDate = DateTime.Now }
};
So I need a List<List<MyObj>> :
List<MyObj> MyObjectwith1AndBMW = new List<MyObj> {
new MyObj() { Number = 1, Name = "BMW", Message = "msg1", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "BMW", Message = "msg2", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "BMW", Message = "msg3", RegisterDate = DateTime.Now }
};
List<MyObj> MyObjectWith1AndHonda = new List<MyObj> {
new MyObj() { Number = 1, Name = "Honda", Message = "msg11", RegisterDate = DateTime.Now },
new MyObj() { Number = 1, Name = "Honda", Message = "msg12", RegisterDate = DateTime.Now }
};
List<MyObj> MyObjectWith2AndBMW = new List<MyObj> {
new MyObj() { Number = 2, Name = "BMW", Message = "msg22", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "BMW", Message = "msg23", RegisterDate = DateTime.Now },
new MyObj() { Number = 2, Name = "BMW", Message = "msg24", RegisterDate = DateTime.Now }
};
List<MyObj> MyObjectWith2AndHonda = new List<MyObj> {
new MyObj() { Number = 2, Name = "Honda", Message = "msg30", RegisterDate = DateTime.Now }
};
List<MyObj> MyObjectwith3AndBMW = new List<MyObj> {
new MyObj() { Number = 3, Name = "BMW", Message = "msg41", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "BMW", Message = "msg42", RegisterDate = DateTime.Now }
};
List<MyObj> MyObjectWith3AndFord = new List<MyObj> {
new MyObj() { Number = 3, Name = "Ford", Message = "msg51", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "Ford", Message = "msg52", RegisterDate = DateTime.Now },
new MyObj() { Number = 3, Name = "Ford", Message = "msg53", RegisterDate = DateTime.Now }
};
the result is :
List<List<MyObj>> result = new List<List<MyObj>> {
MyObjectwith1AndBMW,
MyObjectWith1AndHonda,
MyObjectWith2AndBMW,
MyObjectWith2AndHonda,
MyObjectwith3AndBMW,
MyObjectWith3AndFord
};
SO what is your suggestion? How can I find list of MyObj with same the equal names and equal numbers?
List<List<MyObj>> result = MyObjects.GroupBy(m => new { m.Name, m.Number })
.Select(g => g.ToList())
.ToList();
var groups = (from row in MyObjects
group row by new { row.Number, row.Name } into grp
select grp.ToList()).ToList();
which is a List<List<MyObj>> grouped by .Number and .Name (together).