for (int i = 0; i < 1000; i++)
{
PrivateMessage privateMessage = new PrivateMessage()
{
id=i
};
dc.PrivateMessages.InsertOnSubmit(privateMessage);
dc.SubmitChanges();
}
This method is suitable for insert 1000 records?
There are better ways to do this?
Yeah-- probably best not to do your InsertOnSubmit in a loop. Try:
List<PrivateMessage> messages = new List<PrivateMessage>();
for (int i = 0; i < 1000; i++)
{
messages.Add(new PrivateMessage() { id=i });
}
dc.PrivateMessages.InsertAllOnSubmit(messages);
dc.SubmitChanges();
have dc.SubmitChanges() out of loop.
for (int i = 0; i < 1000; i++)
{
PrivateMessage privateMessage = new PrivateMessage()
{
id=i
};
dc.PrivateMessages.InsertOnSubmit(privateMessage);
}
dc.SubmitChanges();
Here's another one:
Enumerable.Range(1, 1000).ToList().ForEach(x =>
dc.PrivateMessages.InsertOnSubmit(new PrivateMessage(){id=x}));
dc.SubmitChanges();
Related
I'm trying to use for loop inside for loop but it doesn't working i also tried while loop but...
int[] numArray = new int[10] {1,2,2,3,3,4,4,5,5,9};
List<Int32> uNum = new List<Int32>();
/*Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}*/
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] != numArray[i])
{
for (int j = 0; j < numArray.Length-1; j++)
{
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
You have an error in this line:
if (numArray[i] != numArray[i])
This condition will always return False because a number is always equal to itself.
Do something like this:
for (int i=0; i<numArray.Length; i++)
{
int j;
for (j=0; i<numArray.Length; j++){
if (i != j){
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
int[] numArray = new int[10];
List<Int32> uNum = new List<Int32>();
Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}
for (int i = 0; i < numArray.Length; i++)
{
int num = numArray[i];
int count = 0;
for (int j = 0; j < numArray.Length; j++)
{
if (numArray[j] == num)
{
count++;
}
}
if (count == 1)
{
uNum.Add(num);
}
}
May you please help, I can only insert a table with any number of rows, but not columns; I get an E_INVALIDARG when cols > 1 in ITextRange2::InsertTable(cRow,cCol,AutoFit)??
IRichEditOle* pREOle = NULL; // rCtrl is my derived CRichEditView in MFC
HRESULT hr = rCtrl.SendMessage(EM_GETOLEINTERFACE, 0,
(LPARAM)&pREOle);
CComQIPtr<ITextDocument2> pITextDoc;
hr = pREOle->QueryInterface(__uuidof(ITextDocument2), (void*)&pITextDoc);
CComPtr<ITextRange2> range;
pITextDoc->Range2(start, start, &range);
hr = range->InsertTable(3,3, 1); // get hr = E_INVALIDARG here
Below is code, that will insert a table for editing rows or inserting a new table. The function is declared in my derived view class CMHMView. I added fields such as ITextRange2* m_ptr2, HRESULT m_hr.
Initialization is done in my CRichEditView::OnInitialUpdate:
CRichEditCtrl& re = GetRichEditCtrl();
m_pREOle = re.GetIRichEditOle();
m_hr = m_pREOle->QueryInterface(IID_PPV_ARGS(&m_ptd2));
This is the function I use to add the table(s)
void CMHMView::InsertTableRow(long index, long& start, long& end, long ncells,
long cellwidths[], long skip, LPCTSTR* sText, BOOL bAppend){
if (SUCCEEDED(m_hr))
{
long lDelta{};
CComPtr<ITextFont> pFont;
m_hr = m_ptr2->GetFont(&pFont);
CComPtr<ITextRow> pRow;
CComPtr<ITextFont> pFontDuplicate;
m_hr = m_ptr2->GetRow(&pRow);
m_hr = pFont->GetDuplicate(&pFontDuplicate);
if (pRow && pFontDuplicate)
{
CComPtr<ITextPara> pPara, pParaDuplicate;
m_ptr2->GetPara(&pPara);
pPara->GetDuplicate(&pParaDuplicate);
pFontDuplicate->SetName(CComBSTR(L"Courier New"));
pFontDuplicate->SetSize(12.0f);
m_ptr2->MoveStart(tomTable, start, &lDelta);
m_ptr2->Collapse(tomEnd);
if (bAppend)
{
pRow->SetCellCount(ncells);
for (int i = 0; i < ncells; i++)
{
pRow->SetCellIndex(i);
pRow->SetCellWidth(cellwidths[i]);
}
pRow->Insert(1);
m_ptr2->SetIndex(tomTable, index, 0);
for (int i = 0; i < skip; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(L""));
}
for (int i = 0; i < ncells; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(sText[i]));
m_ptr2->SetFont(pFontDuplicate);
m_ptr2->SetPara(pParaDuplicate);
}
m_ptr2->EndOf(tomTable, tomMove, &lDelta);
m_ptr2->GetEnd(&end);
}
else
{
m_ptr2->SetText(CComBSTR(L"\r"));
m_ptr2->Collapse(tomEnd);
pRow->SetCellCount(ncells);
for (int i = 0; i < ncells; i++)
{
pRow->SetCellIndex(i);
pRow->SetCellWidth(cellwidths[i]);
/*pRow->SetCellMergeFlags(tomHStartCell)*/
}
pRow->Insert(1);
m_ptr2->SetIndex(tomTable, index, 0);
for (int i = 0; i < ncells; i++)
{
if (i > 0)
m_ptr2->MoveStart(tomCell, 1, &lDelta);
m_ptr2->SetText(CComBSTR(sText[i]));
m_ptr2->SetFont(pFontDuplicate);
m_ptr2->SetPara(pParaDuplicate);
}
m_ptr2->EndOf(tomTable, tomMove, &lDelta);
m_ptr2->GetEnd(&end);
}
}
}
m_pREOle->Release();}
I hope this will help others!
Unable to run this code, tried using if else statement inside for loop. Want to skip q6:q10 data. Please help. Using it in Google Appscript for google spreadsheet
{ var data1=[];;
for(var i=0; i<dataLen; i++)
for (q = 0;q<20;if (q=5, q+=4; else q++)
{
data1[q]=data[i][q];
}
ss.appendRow(data1);
flag="true";
The format and lack of context makes this question very confusing, but a loop like this solves your stated problem
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 20;q++){
if(q == 7){
q = 10;
}
data1[q] = data[i][q];
}
}
If you would like to use 2 for loops, it would look like
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 7;q++){
data1[q] = data[i][q];
}
}
for(var i=0; i<dataLen; i++){
for(q = 10; q < 20;q++){
data1[q] = data[i][q];
}
}
I don't know what you really want, but I think you want something like this :
For(var i = 0; i<dataLen; i++){
if(i%10 == 6||i%10 == 7||i%10 == 8||i%10 == 9||i%10 == 0){
} else {
data1[i]=data[q][i];
ss.appendRow(data1);
flag = "true";
}
}
Is there a simpler way to rewrite this loop? Something with less code.
Any help is appreciated.
for(int i=0; i< 50; i++){
if(i>=0 && i<10){
method(arr[0]);
}
if(i>=10 && i<20){
method(arr[1]);
}
if(i>=20 && i<30){
method(arr[2]);
}
if(i>=30 && i<40){
method(arr[3]);
}
if(i>=40 && i<50){
method(arr[4]);
}
}
You could use this approach:
for(int i = 0; i < 50; i++){
int index = i == 0 ? 0 : (int)Math.floor(i / 10);
method(arr[index]);
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < 10; j++) {
method(arr[i]);
}
}
You could use a function to increase readability:
for (int i = 0; i < arr.length; i++) {
method10(arr[i]);
}
I would probably rewrite it using the modular function and a counter. This should help reduce the code significantly.
int index;
int count = 0;
for(int i=0; i< 50; i++){
index = i%10;
if(index==0){
count++;
}
method(arr[count-1]);
}
Use else:
for (i=0; i<50: i++) {
if (i<10) {
method(arr[0]);
} else if (i<20) {
method(arr[1]);
} else if (i<30) {
method(arr[2]);
} else if (i<40) {
method(arr[3]);
} else {
method(arr[4]);
}
}
For one liner you can use short if statement. Where the FALSE checks another condition
for (i=0; i<50: i++) {
(condition) ? TRUE : FALSE
}
Below is some core graphics code..
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
colorRefArray already has memory and CreateColor(); will again create a memory and it leads to memory leak.
How do I avoid this situation?
One possible thought I have is
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
colorRefArray[i] = colorref;
CFRelease(colorref);
}
Is this approach correct?
No it's not. You're immediately releasing the color you created. The correct approach would be this:
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
//Use your colors
//Now release them
for (int i = 0; i < MAGIC_NUM ; i++)
{
CFRelease(colorRefArray[i]);
}
No, because then colorRefArray will be filled with invalid pointers.
Try using a CFMutableArray instead of a raw C array. Then you only have to worry about the reference to the array, as it will own the colours for you:
CFArrayRef CopyColorArray(void) {
CFMutableArrayRef colorRefArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
if (colorRefArray) {
for (int i = 0; i < MAGIC_NUM ; i++) {
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
if (colorref) {
CFArrayAppendValue(colorRefArray, colorref);
CFRelease(colorref);
}
}
}
return colorRefArray;
}