i have a question about Entity Framework 6 and ToListAsync()/ToList().
if i have a little amount of data (around 1000 rows) i have noticed that ToListAsync() is faster than ToList().
But if i have a huge amount of data (around 600.000 rows) ToList() is faster than ToListAsync().
Here you can fine the code, someone can explain me why?.
//SharedAnagra contains around 1000 rows
static List<SharedAnagra> GetSharedAnagraList()
{
using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
{
List<SharedAnagra> listaSharedAnagras = (from a in intranet2K3Entities.sharedAnagra
select new SharedAnagra
{
AnagraActive = a.AnagraActive,
AnagraId = a.AnagraID,
AnagraName = a.AnagraName,
OfficeId = a.OfficeID
}).ToList();
return listaSharedAnagras;
}
}
static async Task<List<SharedAnagra>> GetSharedAnagraListAsync()
{
using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
{
List<SharedAnagra> listaSharedAnagras = await (from a in intranet2K3Entities.sharedAnagra
select new SharedAnagra
{
AnagraActive = a.AnagraActive,
AnagraId = a.AnagraID,
AnagraName = a.AnagraName,
OfficeId = a.OfficeID
}).ToListAsync();
return listaSharedAnagras;
}
}
//sharedAnagraNDGClienti contains 950000 rows
static List<SharedAnagraNdgClienti> ListaSharedNdgClienti()
{
using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
{
List<SharedAnagraNdgClienti> lista = (from a in intranet2K3Entities.sharedAnagraNDGClienti
select new SharedAnagraNdgClienti
{
Cm = a.CM,
CodFisc = a.CodFisc,
Cognome = a.Cognome,
Descrizione = a.Descrizione,
FilialePrevalente = a.Filiale_Prevalente,
Ndg = a.NDG,
Nome = a.Nome,
Telefono = a.Telefono,
Tipologia = a.Tipologia,
Trae = a.TRAE,
Tsae = a.TSAE
}).AsNoTracking().ToList();
return lista;
}
}
static async Task<List<SharedAnagraNdgClienti>> GetIssueTypeByIdAsync()
{
List<SharedAnagraNdgClienti> listaSharedAnagraNdgClienti;
using (Intranet2k3Entities intranet2K3Entities = new Intranet2k3Entities())
{
listaSharedAnagraNdgClienti = await (from a in intranet2K3Entities.sharedAnagraNDGClienti
select new SharedAnagraNdgClienti
{
Cm = a.CM,
CodFisc = a.CodFisc,
Cognome = a.Cognome,
Descrizione = a.Descrizione,
FilialePrevalente = a.Filiale_Prevalente,
Ndg = a.NDG,
Nome = a.Nome,
Telefono = a.Telefono,
Tipologia = a.Tipologia,
Trae = a.TRAE,
Tsae = a.TSAE
}).AsNoTracking().ToListAsync();
}
return listaSharedAnagraNdgClienti;
}
I think this is not a problem of your query.This is an issue of EF 6+ framework itself.I personally didn't do any R&D about Asyn vc Sync.But I have found out some useful posts about that.Hope that will help to you.
When to really use async ?
In most applications using async will have no noticeable benefits and
even could be detrimental. Use tests, profiling and common sense to
measure the impact of async in your particular scenario before
committing to it.
Here is that article : Async Query & Save (EF6 onwards)
And here is the Great research done by one of the Stackoveflow user.You can read that too.Great one.
Entity Framework async operation takes ten times as long to complete
Related
In the current project I am working, we are using spark as computation engine for one of workflows.
Workflow is as follows
We have product catalog being served from several pincodes. User logged in from any particular pin code should be able to see least available cost from all available serving pincodes.
Least cost is calculated as follows
product price+dist(pincode1,pincode2) -
pincode2 being user pincode and pincode1 being source pincode. Apply the above formula for all source pincodes and identify the least available one.
My Core spark logic looks like this
pincodes.javaRDD().cartesian(pincodePrices.javaRDD()).mapPartitionsToPair(new PairFlatMapFunction<Iterator<Tuple2<Row,Row>>, Row, Row>() {
#Override
public Iterator<Tuple2<Row, Row>> call(Iterator<Tuple2<Row, Row>> t)
throws Exception {
MongoClient mongoclient = MongoClients.create("mongodb://localhost");
MongoDatabase database = mongoclient.getDatabase("catalogue");
MongoCollection<Document>pincodeCollection = database.getCollection("pincodedistances");
List<Tuple2<Row,Row>> list =new LinkedList<>();
while (t.hasNext()) {
Tuple2<Row, Row>tuple2 = t.next();
Row pinRow = tuple2._1;
Integer srcPincode = pinRow.getAs("pincode");
Row pricesRow = tuple2._2;
Row pricesRow1 = (Row)pricesRow.getAs("leastPrice");
Integer buyingPrice = pricesRow1.getAs("buyingPrice");
Integer quantity = pricesRow1.getAs("quantity");
Integer destPincode = pricesRow1.getAs("pincodeNum");
if(buyingPrice!=null && quantity>0) {
BasicDBObject dbObject = new BasicDBObject();
dbObject.append("sourcePincode", srcPincode);
dbObject.append("destPincode", destPincode);
//System.out.println(srcPincode+","+destPincode);
Number distance;
if(srcPincode.intValue()==destPincode.intValue()) {
distance = 0;
}else {
Document document = pincodeCollection.find(dbObject).first();
distance = document.get("distance", Number.class);
}
double margin = 0.02;
Long finalPrice = Math.round(buyingPrice+(margin*buyingPrice)+distance.doubleValue());
//Row finalPriceRow = RowFactory.create(finalPrice,quantity);
StructType structType = new StructType();
structType = structType.add("finalPrice", DataTypes.LongType, false);
structType = structType.add("quantity", DataTypes.LongType, false);
Object values[] = {finalPrice,quantity};
Row finalPriceRow = new GenericRowWithSchema(values, structType);
list.add(new Tuple2<Row, Row>(pinRow, finalPriceRow));
}
}
mongoclient.close();
return list.iterator();
}
}).reduceByKey((priceRow1,priceRow2)->{
Long finalPrice1 = priceRow1.getAs("finalPrice");
Long finalPrice2 = priceRow2.getAs("finalPrice");
if(finalPrice1.longValue()<finalPrice2.longValue())return priceRow1;
return priceRow2;
}).collect().forEach(tuple2->{
// Business logic to push computed price to mongodb
}
I am able to get the answer correctly, however mapPartitionsToPair is taking a bit of time(~22 secs for just 12k records).
After browsing internet I found that mapPartitions performs better than mapPartitionsToPair, but I am not sure how to emit (key,value) from mapPartitions and then sort it.
Is there any alternative for above transformations or any better approach is highly appreciated.
Spark Cluster: Standalone(1 executor, 6 cores)
public static void updatecasefields(List<Case> lstcase) {
//List<Case> lstcase = new list<case>();
ID devRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('CRM_CSR_Case').getRecordTypeId();
for (Case cs: lstcase) {
if(cs.ID != null && cs.RecordTypeId == devRecordTypeId) {
}
List<CRM_CasePick__c> Casp = [SELECT Id, CRM_Carrier_Name__c,CRM_LOB__c, CRM_SLA_Turnaround_Time__c,CRM_Category__c, CRM_Issue_Sub_Type__c,CRM_Issue_Type__c,CRM_Turnaround_Time_Days__c FROM CRM_CasePick__c WHERE CRM_Carrier_Name__c = :cs.GiDP_CarrierName__c AND CRM_Category__c = :cs.CRM_Category__c AND CRM_Issue_Type__c = :cs.CRM_Issue_Type__c AND CRM_Issue_Sub_Type__c = :cs.CRM_Issue_Sub_Type__c AND CRM_LOB__c = :cs.CRM_Line_of_Business__c];
for(CRM_CasePick__c CP: Casp) {
cs.CRM_Turnaround_Time_Days__c = cp.CRM_Turnaround_Time_Days__c;
cs.CRM_SLA_Turnaround_time__c = cp.CRM_SLA_Turnaround_Time__c;
}
}
}
Remove the SOQL query from the for loop - best practice is to never run a query within a loop.
Right now it is running that query for every value of the initial list. If the list is over 100 records, it will exceed the governor limit.
I am using Core data to store two databases that contain a relationships.
There is a logbook database and a IFRApproaches database.
A logbook entry can have multiple approaches.
An approach can only be linked to one logbook entry.
Attached is screenshots of my data model and relationships.
When the "submit" button is pressed this is the code to save the entry to the logbook, and the approaches as well.. I need the approaches (how ever many) associated with a singular entry. My question is How I can use a predicate to query the database at a later time for all the approaches associated with a various entry.
lazy var newEntry = Logbook(context: LoadData.shared.context)
if errorCount == 0 {
if modifyEntry == false {
if entryHolding.date == nil {
newEntry.date = Date()
} else {
newEntry.date = entryHolding.date
}
newEntry.aircraftID = entryHolding.aircraftID
newEntry.aircraftSelect = LoadData.shared.aircraftArray[aircraftArrayIndexPath!.row]
newEntry.categoryAndClass = newEntry.aircraftSelect?.categoryAndClass
newEntry.typeCode = entryHolding.typeCode
newEntry.from = entryHolding.from
newEntry.to = entryHolding.to
newEntry.route = entryHolding.route
newEntry.totalTime = entryHolding.totalTime
newEntry.pic = entryHolding.PIC
newEntry.sic = entryHolding.SIC
newEntry.solo = entryHolding.solo
newEntry.dual = entryHolding.dual
newEntry.crossCountry = entryHolding.crossCountry
newEntry.night = entryHolding.night
newEntry.simInstrument = entryHolding.simInstrument
newEntry.actualInstrument = entryHolding.actualInstrument
newEntry.hold = Int16(entryHolding.hold)
newEntry.dayLanding = Int16(entryHolding.dayLanding)
newEntry.nightLanding = Int16(entryHolding.nightLanding)
newEntry.bfr = entryHolding.BFR
newEntry.checkride = entryHolding.checkRide
newEntry.ipc = entryHolding.IPC
newEntry.comments = entryHolding.comments
for i in 0..<approach.count {
var newApproach = IFRApproaches(context: LoadData.shared.context)
newApproach.airport = approach[i].airportID
newApproach.typeOfApproach = approach[i].type
newApproach.runway = approach[i].runwayNum
//newApproach.entry = newEntry
newEntry.addToApproaches(newApproach)
LoadData.shared.approachArray.append(newApproach)
}
LoadData.shared.entryArray.append(newEntry)
LoadData.shared.saveData()
I am populating a dataGridView from an incomplete excel spreadsheet and generating the LiteDB from the dataGridView. Before the recent update, I wasn't having any issues. Now I am getting the error 'Objects cannot be cast from DBNull to other types'. I can work around this by including dummy values in the original spreadsheet. But ultimately we need to see what information we're missing. How can I accomplish this?
public void CreateDatabase()
{
using (var db = new LiteDatabase(#"C:\Users\BThatcher\Documents\_Personal\Revit\MDL-Sheets\SheetDatabase04.db")) //< ----------------
{
var sheets = db.GetCollection<Sheet>("sheets");
foreach (DataGridViewRow row in dataGridView2.Rows)
{
var sheet = new Sheet
{
SheetSequence = Convert.ToInt32(row.Cells[0].Value),
SheetNumber = row.Cells[1].Value.ToString(),
SheetName = row.Cells[2].Value.ToString(),
AreaNumber = Convert.ToInt32(row.Cells[3].Value),
AreaName = row.Cells[4].Value.ToString(),
SheetDiscipline = row.Cells[5].Value.ToString(),
DisciplineSort = Convert.ToInt32(row.Cells[6].Value),
SheetSort = Convert.ToInt32(row.Cells[7].Value),
ModelName = row.Cells[8].Value.ToString(),
AppearsInSheetlist = row.Cells[9].Value.ToString(),
DesignedBy = row.Cells[10].Value.ToString(),
DrawnBy = row.Cells[11].Value.ToString(),
CheckedBy = row.Cells[12].Value.ToString(),
ApprovedBy = row.Cells[13].Value.ToString(),
SheetTotal = Convert.ToInt32(row.Cells[14].Value),
DesignSoftware = row.Cells[15].Value.ToString(),
HasPdf = row.Cells[16].Value.ToString(),
PdfPath = row.Cells[17].Value.ToString(),
};
sheets.Insert(sheet);
this.Close();
}
}
}
This error are not from LiteDB (there is no DBNull in LiteDB). It's possible this DBNull are getting from row.Cells[n].Value when you try to convert.
LiteDB support nulls and nullable values (like int?).
I'm trying to use UA-.NETStandardLibrary by OPC Foundation to create my own OPC UA Server that will maintain some variables.
I've created a server class inherited from StandardServer and node manager inherited from CustomNodeManager2.
There were some node managers in their examples, I removed them and add my own one. The server starts normally and doesn't contain any nodes except from standard ones, as planned. So, my problem is how to create my own variable node from code (not from xml, as in examples) and be able update its value on demand.
For example, I want to add a folder with couple of nodes inside.
Does anyone have a code snippet which demonstrates how to do it? I don't want anybody write it for me, I will appreciate only if you just tell me about a right way to make it.
Thanks a lot.
I am pretty sure the snippets you are looking for are included. Here is my testing code and I am 100% positive, I didn't write the second piece of code. Anyway, if this helps you...
{
var ticker_seq = createVariable(myFolder, "MyFolder/Ticker", "Ticker", BuiltInType.UInt64, ValueRanks.Scalar);
variables.Add(ticker_seq);
subscriptions.Add(clock.Ticker.Subscribe(val =>
{
lock (Lock)
{
ticker_seq.Value = val;
ticker_seq.Timestamp = DateTime.UtcNow;
ticker_seq.ClearChangeMasks(SystemContext, false);
}
}));
}
and creation
private BaseDataVariableState createVariable(NodeState parent, string path, string name, NodeId dataType, int valueRank)
{
BaseDataVariableState variable = new BaseDataVariableState(parent);
variable.SymbolicName = name;
variable.ReferenceTypeId = ReferenceTypes.Organizes;
variable.TypeDefinitionId = VariableTypeIds.BaseDataVariableType;
variable.NodeId = new NodeId(path, NamespaceIndex);
variable.BrowseName = new QualifiedName(path, NamespaceIndex);
variable.DisplayName = new LocalizedText("en", name);
variable.WriteMask = AttributeWriteMask.DisplayName | AttributeWriteMask.Description;
variable.UserWriteMask = AttributeWriteMask.DisplayName | AttributeWriteMask.Description;
variable.DataType = dataType;
variable.ValueRank = valueRank;
variable.AccessLevel = AccessLevels.CurrentReadOrWrite;
variable.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
variable.Historizing = false;
variable.Value = 0;
variable.StatusCode = StatusCodes.Good;
variable.Timestamp = DateTime.UtcNow;
if (parent != null)
{
parent.AddChild(variable);
}
return variable;
}
creating the folder:
private FolderState CreateFolder(NodeState parent, string path, string name)
{
FolderState folder = new FolderState(parent);
folder.SymbolicName = name;
folder.ReferenceTypeId = ReferenceTypes.Organizes;
folder.TypeDefinitionId = ObjectTypeIds.FolderType;
folder.NodeId = new NodeId(path, NamespaceIndex);
folder.BrowseName = new QualifiedName(path, NamespaceIndex);
folder.DisplayName = new LocalizedText("en", name);
folder.WriteMask = AttributeWriteMask.None;
folder.UserWriteMask = AttributeWriteMask.None;
folder.EventNotifier = EventNotifiers.None;
if (parent != null)
{
parent.AddChild(folder);
}
return folder;
}