When and why to insert Entity and its relational Entity references - entity-framework-6

i am building a function for a bulk import from a file and i am asking myself what i should do, i would like some feedback and maybe learn certain things for the future.
I have 3 Entities, lets call them E1, E2 & E3.
E1 has a property E2 and E2 has a property E3. I can build everything up (doing checks and so on) and insert E3, which will insert E1 & E2 (IF they don't already exist of course otherwise i would attach them).
But why shouldn't i for example build up E1 and insert it, continue build E2 and insert it, lastly build up E3 (with E1 & E2) and insert it ?
Is there a specific reason i shouldn't do that, to many calls maybe, is it a "no-go", ... ?
Kind regards!

The most important thing is to call SaveChanges on the Context at the very end, when all entities are added. This way, all entities are inserted/updated in one Transaction and there are as few database-calls as possible.
The benefits are good performance and atomic-Transactions ( all succeeds or all fails).

Related

Run filter in each row dynamically

I'm trying to manage a dynamic set of data over the course of several steps, and at each step I need to remove a specific value from each row, and shift the remaining values to the left to fill in any blanks.
I know I can use
=arrayformula(if(range=value, "", range))
to remove the specific value, but I've only been able to find
=filter(row, row<>"")
to fill in the blanks, but since the number of rows changes, this isn't going to work.
Edit:
I want to turn something like this
Column A
Column B
Column C
Data A1
Data B1
Data C1
Data A2
Data B2
Data C2
Data A3
Data B3
Data C3
Data A4
Data B4
Data C4
etc
etc
etc
to this
Column A
Column B
Column C
Data A1
Data C1
Data A2
Data B2
Data C2
Data B3
Data C3
Data C4
etc
etc
etc
Since I only know how many columns there are, I can only pre-set the formula to filter each column, which moves data vertically. I want to move it horizontally, but where the number of rows is variable, I can't manually pre-set each row to use filter.
Solution: applying FILTER() in combination with BYROW(). Because Google's BYROW() is unfortunately limited to a single value return from it's LAMBDA(), we have to rely on the old ARRAYFORMULA/JOIN/SPLIT trick.
=arrayformula(
split(
byrow(A1:C4,
lambda(row,
ifna(
join(",",
filter(row, row<>"A")),
""))),
","))
As of today (23-Jan-2023), it appears that Google has updated the BYROW/BYCOL functions to allow the return of an array result per row/column, so the answer to the OPs question could now simply be =byrow(range,lambda(row,filter(row,row<>""))).
I have not seen this documented anywhere, and the help pages for the BYROW & BYCOL functions still state that only a single value per row/column is allowed.
Slightly off-topic, but I can also confirm that you can pass the individual row/columns from a BYROW/BYCOL into the other LHFs as well (e.g. a SCAN within a BYROW to give a row-wise running total, etc.)

Filter sheets column based on cell with multiple values

I am working on a spells info list for a dungeons and dragons google sheets and a basic filter by values does not meet my needs. Filter by values would work if I was only going to have a single value in the cell, but I was going to be recording both class and spell level, so Wizard 1 would be W1 and Druid 1 would be D1. The reason why filter by values does not work is because a spell belongs on different spell lists at different levels. So if I was to make a multiclass character, I would like to be able to say show all that contain any of (W1, W2, C1) because I am a 3rd level wizard and 1st level cleric.
I know I could do this by having each class with its own column as I was doing this before, but I was looking for a more complete solution.
Spell Name
Spell Lists
Mage Armor
W1, S1
Resistance
C0, D0, W0, S0
Shield of Faith
C1, D1
Choosing only W0 would give
Spell Name
Spell Lists (W0)
Resistance
C0, D0, W0, S0
I did this using the QUERY() function although I am sure there are other solutions
=query(A1:B4, "SELECT A, B WHERE LOWER(B) LIKE LOWER('%"&JOIN("%') OR LOWER(B) LIKE LOWER('%", SPLIT(B11, ","))&"%')", 1)
This is on a sample sheet I created
https://docs.google.com/spreadsheets/d/1qbLOjTdzISICTKyUp_jK6gZbQCt-OwtDYYy3HNJygeE/edit#gid=1358975023
With your current sample data FILTER() would give you desired result.
=FILTER(A2:B4,ISNUMBER(SEARCH("W0",B2:B4)))

CREATE RELATIONSHIP in cypher based on a conditions

I have two dates D1, D2 and a list of dates - DATESLIST. I want to create a relationship only when two conditions are satisfied.
All dates between D1 and D2 are not in DATESLIST
All dates between D1 and D2 are not weekends
the psuedocode of the same would look something like:
flag = 0
for d in range(D1, D2):
if d not in DATESLIST and not d.isweekend():
flag = 1
break
if flag == 0:
CREATE RELATIONSHIP
D1, D2 and DATESLIST are all obtained from as a result of a MATCH query.
I have thought of using apoc.do.when() along with a CALL construct, but I am unable to think of the syntax that would help me implement the flag and break logic.
Thanks in advance.
Please let me know if you have any questions.
My comments is that not all tools are suitable for all problems. Use the right tool for the job.
I would take your pseudo code and write the solution in another language (Java, Python, even BASH), and have this write to the DB. That way you can easily test it before hand to make sure the code is doing what you expect.

EPL pattern to match latest A and all Bs with filter

I am trying to find an EPL pattern that will match the latest A followed by any & all B(prop1=A.prop1).
Using the same sequence as defined in Esper's documentation...
A1 B1 C1 B2 A2 D1 A3 B3 E1 A4 F1 B4
I am looking for a pattern that will match {A1,B1} {A1,B2} {A3,B3} {A4,B4} assuming that they all have the same prop1 values.
So to be precise, after the first match I do not want the pattern matcher to restart and look for the next A event.
Any help gratefully received!
Having run some tests around this, it turns out that the answer is a filtered version of the every A -> every B as already included in the Esper samples...
select * from pattern [every a=A -> every b=B(prop1 = a.prop1) and not A(prop1=a.prop1)]

Sorting Military Ranks then having output based on that rank in the next column. If statements possibly involved

If E7 and above (which includes E7 to E9 and O1 to O9), output MANAGER. If E6 and below (which include E1 to E6), output Worker Bee. I don't want to change Col A's order to make Col B. This is an example that would be for much longer lists so I need it to be automatic. Any ideas? I tried multiple If statements but they kept recognizing the cell and not the rank. Thank you!
Col A Col B
O6 MANAGER
E7 MANAGER
E3 Worker Bee
E1 Worker Bee
04 MANAGER
=IF(LEFT(A3,1)="O","MANAGER",IF(VALUE(RIGHT(A3,1))>=7,"MANAGER","Worker Bee"))
Put this in cell B1 and auto fill down as required.
I wrote this formula on my way home on the train using my mobile phone so it is untested , for any reason it's not working then use it as a starting point for your own formula.
Edit:
Formula now working. I have tested and updated the formula, I have changed it so it now accounts for the number after the letter being treated as text instead of a number value.

Resources