I'm working on the DbSet where I'm modifying it and filtering data I have modified before saving those changes to database.
Initial content of table:
Id,Name,Left,Right
1,All,1,28
2,Press,2,13
3,NewDeals,3,4
4,Contracts,5,6
5,People,7,12
6,Promotions,8,9
7,Deaths,10,11,
8,Finanse,14,19
9,Results,15,16,
10,Stocks,17,18
11,Stories,20,25
12,SuccessStories,21,22,
13,LearnStories,23,24
14,AccessToEverything,26,27
Steps looks like this:
//items are DbSet
var nodeLeft = 10;
var nodeRight = 11;
var parentNodeRight = 28;
var size = 2;
var itemsToUpdate = items.Where(i => i.Left >= nodeLeft && i.Right <= nodeRight);
foreach (var item in itemsToUpdate)
{
item.Left = 0 - item.Left;
item.Right = 0 - item.Right;
}
// step 2
itemsToUpdate = items.Where(i => i.Left > nodeRight);
foreach (var item in itemsToUpdate)
{
item.Left = item.Left - size;
}
itemsToUpdate = items.Where(i => i.Right > nodeRight);
foreach (var item in itemsToUpdate)
{
item.Right = item.Right - size;
}
//step 3
itemsToUpdate =
items.Where(i => i.Left >= (parentNodeRight > nodeRight ? parentNodeRight - size : parentNodeRight));
foreach (var item in itemsToUpdate)
{
item.Left = item.Left + size;
}
At step 3, where statement is returning values which does not fulfill the statement in where.
It looks like EF is filtering the data which are not in memory but in DB.
If I change items to regular list by applying toList() everything is working fine.
I would really like to avoid that since data set I'm working on can be very big and I can not initially pre-filter it.
Could you please advise what I'm missing / doing wrong here ?
Related
In my project, necessary to paste from word.docx where many elements, for example table. I got cleanup attributes "width" of the inserted tables like this:
paste_postprocess: function(editor, fragment) {
var allTables = fragment.node.getElementsByTagName('table');
for (let i = 0; i < allTables.length; ++i) {
var tab = allTables[i].removeAttribute('width');
}
Are there any methods"paste_postprocess" to wrapping all the inserted tables in "div" ?
Thanks!
Ok, I found way
var tables = fragment.node.getElementsByTagName('table');
for (let i = 0; i < tables.length; ++i) {
var div = document.createElement("div");
div.className = "table_scroll";
var tablesdiv = tables[i].parentNode.insertBefore(div, tables[i]);
div.appendChild(tables[i]);
}
seems to work
I am able to successfully download excel files from my JSON array. The array has many records and based on user input, it is decided if to download all data in one file or multiple. If the action is Yes, then I want to download the data in chunks of 25 rows into multiple files.
Below is my code for download. If there are 75 records then 4 files are downloading. First 3 are correct with 25 records each, but the 4th file is exact duplicate of the 3rd file. In other words, the last file always exports twice even if there are only 2 records, I am still getting 2 files.
How to avoid this issue?
A controller
aProducts =
this.getView().getModel("orderMaterials").getProperty("/MaterialData");
var oModel = [];
for (var i = 0; i <= aProducts.length - 1; i++) {
var items = {};
items.SubmittedBy = submittedBy;
items.MaterialNo= aProducts[i].MaterialNo;
items.LineNumber = i + 1;
oModel.push(items);
}
if (sAction === "YES") {
var i, j, temparray, chunk = 25;
for (i = 0, j = oModel.length; i < j; i += chunk) {
temparray = oModel.slice(i, i + chunk);
oSettings = {
workbook: {
columns: aCols
},
dataSource: temparray
};
var oSpreadsheet = new sap.ui.export.Spreadsheet(oSettings);
oSpreadsheet.build().then(function () {
sap.m.MessageToast.show("Spreadsheet export has finished");
});
}
I have this code:
function showForm()
{
var a=document.getElementById("opts").value;
if(a==1)
{
document.getElementById("f1").style.display="block";
document.getElementById("f2").style.display="none";
document.getElementById("f3").style.display="none";
document.getElementById("f4").style.display="none";
document.getElementById("f5").style.display="none";
document.getElementById("f6").style.display="none";
document.getElementById("f7").style.display="none";
document.getElementById("f8").style.display="none";
document.getElementById("f9").style.display="none";
document.getElementById("f10").style.display="none";
document.getElementById("f11").style.display="none";
document.getElementById("f12").style.display="none";
document.getElementById("f13").style.display="none";
document.getElementById("f14").style.display="none";
document.getElementById("f15").style.display="none";
document.getElementById("f16").style.display="none";
document.getElementById("f17").style.display="none";
document.getElementById("f18").style.display="none";
document.getElementById("f19").style.display="none";
document.getElementById("f20").style.display="none";
document.getElementById("f21").style.display="none";
document.getElementById("f22").style.display="none";
document.getElementById("f23").style.display="none";
document.getElementById("f24").style.display="none";
document.getElementById("f25").style.display="none";
document.getElementById("f26").style.display="none";
document.getElementById("f27").style.display="none";
document.getElementById("f28").style.display="none";
document.getElementById("f29").style.display="none";
document.getElementById("f30").style.display="none";
document.getElementById("f31").style.display="none";
document.getElementById("f32").style.display="none"
}
if(a==2)
{
//...
}
}
Is it possible to get this js to be smaller?
I use it for the website www.borrani.com in the double dropdown selector
A loop could certainly make it smaller:
for (var i = 1; i <= 32; i++) {
document.getElementById('f' + i).style.display = 'none';
}
document.getElementById('f1').style.display = 'block';
You might even try querySelectorAll to explicitly identify your elements based on a pattern, assuming you can define a unique pattern for only the elements you want:
var elements = document.querySelectorAll('[id^="f"]');
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
document.getElementById('f1').style.display = 'block';
It may be slightly more code, but it de-couples the code from the specific number of elements being identified.
I've written some code that translates an Entity Framework collection to some fixed fields. I ended up with the following snippet but isn't there a slicker way to accomplish this?
var numbers = c.ContactPhoneNumbers.OrderByDescending(n => n.IsPrimary);
int count = 0;
foreach (var number in numbers)
{
if (count == 0)
{
hc.PrimaryPhone = number.PhoneNumber;
hc.PrimaryPhoneType = number.PhoneNumberType;
}
else if (count == 1)
{
hc.SecondaryPhone = number.PhoneNumber;
hc.SecondaryPhoneType = number.PhoneNumberType;
}
else break;
count++;
}
c is an Entity Framework entity and c.ContactPhoneNumbers represents entries in a related table. Seems like this code could be made a little more straight forward and less awkward.
Since you are iterating the phone enumeration right away, might be better to use ToList() so you can use the indexer:
var numbers = c.ContactPhoneNumbers.OrderByDescending(n => n.IsPrimary).ToList();
if(numbers.Count > 0)
{
hc.PrimaryPhone = numbers[0].PhoneNumber;
hc.PrimaryPhoneType = number[0].PhoneNumberType;
}
if(numbers.Count > 1)
{
hc.SecondaryPhone = numbers[1].PhoneNumber;
hc.SecondaryPhoneType = numbers[1].PhoneNumberType;
}
This is driving me nuts. What am I missing here. I'm using EF and if I have code like the following:
using (LexiconEntities ctx = new LexiconEntities())
{
var query = from w in ctx.Words
select new WordEntryDataModel
{
Word = w.Anagram,
NumOfAnagrams = w.NumAnagrams.Value,
Length = w.Length.Value,
...
};
SearchCriterion c1 = new SearchCriterion();
SearchCriterion c2 = new SearchCriterion();
c1.MinValue = 3;
c1.MaxValue = 3;
c2.MinValue = 4;
c2.MaxValue = 4;
query = query.Where(w => w.Length >= c1.MinValue && w.Length <= c1.MaxValue);
query = query.Where(w => w.NumOfAnagrams >= c2.MinValue && w.NumOfAnagrams <= c2.MaxValue);
...
}
And when I debug the query, I get the proper results (8 records). This also works as expected in Linqpad (which frickin' rocks).
But if I construct the search criteria as a List of criterion objects, and I dynamically add on Where() clauses by iterating over the search criteria as follows:
foreach (SearchCriterion c in criteria.SearchCriteria)
{
switch (c.Type)
{
case SearchCriterionType.WordLength:
query = query.Where(w => w.Length >= c.MinValue && w.Length <= c.MaxValue);
break;
case SearchCriterionType.NumberOfAnagrams:
query = query.Where(w => w.NumOfAnagrams >= c.MinValue && w.NumOfAnagrams <= c.MaxValue);
break;
...
case SearchCriterionType.NumberOfVowels:
query = query.Where(w => w.NumOfVowels >= c.MinValue && w.NumOfVowels <= c.MaxValue);
break;
}
}
...
I get the totally different (and incorrect) results. I've debugged the switch statement and my search criteria has two properly constructed criterion objects set to correct values. There's something about the conditionally added where clauses that my query doesn't like.
What am I doing wrong?
Closure. Assign c to a local variable within the loop. Also see my SO answer here