Get max ID value with Entity Framework in C# - entity-framework

With the following JSON I want to get the max value of the email ID.
[
{
"id": 1,
"firstName": "Bill",
"lastName": "Smith",
"emails": [
{
"id": 1,
"email": "jack#jackkkfgfkfgkkkkkk.com",
"dateCreated": "2017-05-14T14:04:12.3299297-07:00"
},
{
"id": 2,
"email": "jack#jackkkfkkkkkk.com",
"dateCreated": "2017-05-14T14:04:24.1534621-07:00"
},
{
"id": 3,
"email": "jack#jackfkkkkkk.com",
"dateCreated": "2017-05-14T14:04:29.8526171-07:00"
}
]
},
{
"id": 2,
"firstName": "Bill",
"lastName": "Smith",
"emails": [
{
"id": 4,
"email": "jack#jkkkkk.com",
"dateCreated": "2017-05-14T14:04:45.8674213-07:00"
}
]
}
]
My code so far
var maxId = People.Select(p => p.Emails.Max(i => i.Id)).FirstOrDefault();
This however always returns 4, because it only check the first element. Is there a way to check all in a single linq statement? So the next value would be 5.

You want the Max Id of all the emails.
var maxId = People.SelectMany(p => p.Emails).Max(e => e.Id);
var nextId = maxId++;
Use SelectMany to flatten all the emails into one sequence and then get the Max Id from that list.

Related

how to push a data into an array of objects in mongodb based on the name into the the completed array in the breakfast array how will it be done?

I want to add a current date into a user meal plan breakfast and find
the name of an object like a banana on condition and add a date to the
completed array?
this is the mongoose document .every food item contains an array of completed and every time a request is sent it will add or remove date on the completed array. i should be able to push date to the completed array
{
"_id": {
"$oid": "616f193e9eb4ca14afc47fa8"
},
"date": {
"$date": "2021-10-19T15:52:09.298Z"
},
"email": "john#live.com",
"firstName": "john",
"lastName": "doe",
"password": "$2b$10$jKylT8MKsUl7ZS03EVi7SecQ1eWuB37J5rLVTU/C8VXBwkWQQo6EK",
"role": "user",
"token": "eyJhbGciOiJIQb-JA2kcKxWquDaM",
"mealplan": {
"dietType": ["Halal", "Vegetarian", "Vegan"],
"religionType": ["Agnostic", "Muslim", "Christian", "Buddhist"],
"ethinicType": ["Asian", "Native American"],
"_id": "6179a178ba48312626059d44",
"name": "first meal",
"budget": 2000,
"orac": 163,
"medicinal": 0,
"breakfast": [
{
"type": "breakfast",
"completed": [],
"fdcId": 1103194,
"name": "Carrots, raw, salad",
"numbers": 1,
"unit": "100 grams",
"recurring": {
"startTime": "08:30",
"endTime": "09:30",
"startDate": "2021-10-28",
"endDate": "2021-11-03",
"daysofweek": []
}
},
{
"type": "breakfast",
"completed": [],
"name": "orange juice",
"numbers": 1,
"unit": "100 mililiters",
"recurring": {},
"orac": 10,
"medicinal_value": 0,
"nutrition": []
},
{
"type": "breakfast",
"name": "banana",
"completed": [],
"numbers": 1,
"unit": "100 grams"
}
]
},
"__v": 0
}
when I send a post request it will be the name of the food and date.
date show be added to the completed array in name of food
use arrayFilter like this
for example we want add to food with name of bnana
db.collection.update({},
{
$push: {
"mealplan.breakfast.$[arr].completed": "date" // put date here
}
},
{
arrayFilters: [
{
"arr.name": "banana" // name of food here
}
]
})
https://mongoplayground.net/p/lbzcF0YPuDF

how to update the first object in the array of quarterFinals that matches some mongoDB condition?

I have the document below and I need to iterate through the quarterFinals array and update the first null address property I find to '123'
{
"data": {
"createTournament": {
"_id": "613122d0c7befdeef3b0b571",
"title": "Jogo de truco",
"description": "",
"location": "Batalha na rua",
"type": "Battle",
"players": [],
"status": "PENDING",
"size": 8,
"entryFee": 1,
"prizePool": 20,
"currency": "USD",
"startDate": "2021-09-01",
"endDate": "2021-09-01",
"rounds": {
"quarterFinals": [{
"id": "9fb6f9ca-c06a-4972-b140-0f2425a90707",
"arena": "",
"firstParticipant": {
"address": null,
"battlesWon": 0
},
"secondParticipant": {
"address": null,
"battlesWon": 0
}
}]
}
}
}
}
One way of doing this is
exports.updateData = async (tournamentID,address)=>{
return await db_collection.updateOne({id:tournamentId,rounds:{$elemMatch:{quarterfinals.address:null}}},{$push:{address:address});
};

In Netsuite REST, how do I assign an item location to an item line in a sales order?

I'm using Netsuite's Postman collection (which takes care of the Oauth1 stuff), and am POSTing to this endpoint:
{{proto}}://{{host}}/rest/platform/{{version}}/record/salesorder
... and the body is something like this:
{
"customForm": "999",
"entity": {
"id": "1111"
},
"department": {
"id": "2222"
},
"subsidiary": {
"id": "33"
},
"otherRefNum": "TEST-PO",
"location": {
"id": "444"
},
"item": {
"items": [
{
"item": { "id": "555555" },
"inventorylocation": { "id": "444" },
"price": { "id": "-1" },
"grossAmt": 999,
"quantity": 1
}
]
}
}
I'm trying to assign a location on the item level. The above request creates a sales order ok (without the line-level location assignment) if I remove the inventorylocation line, but with that in there, I get this error: Unknown reference or subrecord field inventorylocation in request body.
Netsuite's REST API documentation is here:
https://system.netsuite.com/help/helpcenter/en_US/APIs/REST_API_Browser/record/v1/2019.2/index.html#tag-salesorder
I have also tried substituting location and moving the fields around a bit, without success. (either the salesorder is created without a line-level location assignment, or I get an error similar to the above error.
Any ideas?
From the documentation you linked, it appears that the field id you need is inventorylocation rather than itemlocation.
salesorder-itemElement
...
giftCertRecipientName Recipient Name: string
id [Missing Label:id]: string
inventorydetail: salesorder-item-inventorydetail
inventorylocation: location
inventorysubsidiary: subsidiary
isClosed Closed: boolean
...
Based on the documentation for a salesOrder-itemElement, it looks like that key is correct.
Have you tried the "location": "{ID}" variation?
In LedgerSync it looks like the request for creating an invoice results in this body:
{
"entity": "309",
"location": "1",
"sublist": {
"items": [
{
"amount": 12345,
"description": "Test Line Item FLURYAOLJLFADYGR-1"
},
{
"amount": 12345,
"description": "Test Line Item FLURYAOLUFUTBYJD-2"
}
]
}
}
There also is a salesOrder-item-inventorydetail object that also contains a location. Perhaps you could use that one like so:
{
"customForm": "999",
"entity": {
"id": "1111"
},
"department": {
"id": "2222"
},
"subsidiary": {
"id": "33"
},
"otherRefNum": "TEST-PO",
"location": {
"id": "444"
},
"item": {
"items": [
{
"item": { "id": "555555" },
"inventorydetail": {
"location": "444"
},
"price": { "id": "-1" },
"grossAmt": 999,
"quantity": 1
}
]
}
}

What is the best way to store collection data in a realation?

I would create a react-native app. And now i'am not sure, what is the best way to structure the data in a collection.
Follow scenario i have:
I have two collection companies and users. Both collection have membership contract. So what is the best way to store the data?
Method 1:
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
"subcollection_company": [
{
"id": 1,
"name": "Company LLC",
"membership_status": "REQUESTD",
}
],
],
companies: [
"id": 1,
"name": "Company LLC",
"subcollection_users": [
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"membership_status": "REQUESTED",
}
],
],
}
Method 2 (array instead of subcollections):
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
"array_company": [
{
"id": 1,
"name": "Company LLC",
"membership_status": "REQUESTED",
}
],
],
companies: [
"id": 1,
"name": "Company LLC",
"array_users": [
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"membership_status": "REQUESTED",
}
],
],
}
Method 3 (the good old way like sql):
{
users: [
"id": 1,
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
],
companies: [
"id": 1,
"name": "Company LLC",
],
user_comany: [
{
userref: 'users/1',
companyref: 'companies/1',
"membership_status": "REQUESTD",
}
]
}
According to me method 1 is the best way.
Method 2 will fail for very large datasets. Say a company has lakhs of users, then 16mb limit of the individual document will breach and you will not be able to add more users to it.
MongoDB is not much optimized for joins and in Method 3, you need to join at two levels to fetch information.

Fetch result from json array column in postgres

I have PostgreSQL 9.5 table(instrument) having 2 columns instrument_id and user_maps as shown below:
I want to fetch all instruments based on following conditions:
Loop through each json object in user_maps
count the number of json objects having status in Y, N or D
Count should be more than 2.
Note: user_maps is an array of json object having 2 fields status and userId
Sample user_maps linked to instrument_id "I01":
[
{
"status": "Y",
"userId": "ZU201707120539150007"
},
{
"status": "D",
"userId": "ZU201707120540510008"
},
{
"status": "I",
"userId": "ZU201707120542540009"
},
{
"status": "I",
"userId": "ZU201707011725050001"
},
{
"status": "Y",
"userId": "ZU201707120552050013"
}
]
Instrument id "I01" should come in final result .
Another, Sample user_maps linked to instrument_id "I02":
[
{
"status": "I",
"userId": "ZU201707120539150007"
},
{
"status": "I",
"userId": "ZU201707120540510008"
},
{
"status": "I",
"userId": "ZU201707120542540009"
},
{
"status": "I",
"userId": "ZU201707011725050001"
},
{
"status": "Y",
"userId": "ZU201707120552050013"
}
]
Instrument id "I02" should not come in final result beacuse it has only one json having status in (Y, N,D).
If I understood correctly your request then this is how you can do it:
-- This is just test dataset as you provided
WITH test( instrument_id, user_maps ) AS (
VALUES
( 'I01'::text,
$$[
{ "status": "Y", "userId": "ZU201707120539150007" },
{ "status": "D", "userId": "ZU201707120540510008" },
{ "status": "I", "userId": "ZU201707120542540009" },
{ "status": "I", "userId": "ZU201707011725050001" },
{ "status": "Y", "userId": "ZU201707120552050013" }
]$$::jsonb ),
( 'I02'::text,
$$[
{ "status": "I", "userId": "ZU201707120539150007" },
{ "status": "I", "userId": "ZU201707120540510008" },
{ "status": "I", "userId": "ZU201707120542540009" },
{ "status": "I", "userId": "ZU201707011725050001" },
{ "status": "Y", "userId": "ZU201707120552050013" }
]$$::jsonb )
)
SELECT t.instrument_id,
count( u )
FROM test t,
jsonb_array_elements( user_maps ) u -- does lateral join to get json array elements
WHERE u -> 'status' ?| ARRAY['Y', 'N', 'D'] -- your search condition
GROUP BY 1
HAVING count( u ) > 2; -- the count condition you wanted
-- This is the result of the query
instrument_id | count
---------------+-------
I01 | 3
(1 row)