Adding in IList using for Loop - c#

I'm have list of result as
var attributeresult= " some list of items......";
Now I'm trying to loop form result and adding into IList.but I'm getting only lastly inserting values,but I want to get all the values.
IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
DynamicColumn dynamictableColumns = new DynamicColumn();
for (int i = 0; i < attributeresult.Count(); i++)
{
dynamictableColumns.Name = attributeresult.ElementAt(i).AttributeName;
dynamictableColumns.Type = attributeresult.ElementAt(i).AttributeSqlType;
dynamictableColumns.IsNullable = false;
idynamicttableColumns.Add(dynamictableColumns);
}
I have to accomplish with for loop only not with for each loop.

Move DynamicColumn dynamictableColumns = new DynamicColumn(); into your loop:
IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
int count = attributeresult.Count();
for (int i = 0; i < count; i++)
{
var item = attributeresult.ElementAt(i);
DynamicColumn dynamictableColumns = new DynamicColumn();
dynamictableColumns.Name = item .AttributeName;
dynamictableColumns.Type = item .AttributeSqlType;
dynamictableColumns.IsNullable = false;
idynamicttableColumns.Add(dynamictableColumns);
}

Related

How to add a instance of class into a variable c#?

How to add a instance of class into a variable c#?
for (int i = 0; i < 8; i++)
{
var msg = new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
};
// What I need to do to acumulate msg variable into a new variable?
}
Append the object to a list that exists outside of the loop, instead of just to a variable that only exists inside the loop. For example:
var msgs = new List<Param>();
for (int i = 0; i < 8; i++)
{
msgs.Add(new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
});
}
// here you have the list of Param objects created in your loop
You can create a list of Param
var listParam = new List<Param>();
for (int i = 0; i < 8; i++)
{
var msg = new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
};
listParam.Add(msg);
}
const int count = 8;
var messages = new Param[count];
for (int i = 0; i < count; i++)
{
var msg = new Param
{
type = "text",
text = $"{ message[i].VlrParam.Replace("\n", "").Replace("\r", "")}"
};
messages[i] = msg;
}

C#: Shift given rows of jagged array to the top

I make a tetris game where i need to clear lines. I collect the full rows and make them empty by doing:
for (var i = 0; i < playingGrid.Length; i++) //get rows
{
if (!playingGrid[i].Contains(0)) //check if all colls are filled
{
playingGrid[i] = new int[10]; //make the row empty
//Here i need to push this row to the top of the jagged array
}
}
//playingGrid:
public int[][] playingGrid = new int[20][];
for (var i = 0; i < 20; i++)
{
playingGrid[i] = new int[10];
}
My question is how can i get the row that i cleared at the top of the jagged array?
You should play with indexes to move full rows. Also, no need to use int array as the cell (column) can have only 2 states which is the perfect case for using bool:
var rowsCount = 20;
var columnsInRowCount = 10;
var playingGrid = new bool[rowsCount][];
for (var rowIndex = 0; rowIndex < playingGrid.Length; rowIndex++)
{
playingGrid[rowIndex] = new bool[columnsInRowCount];
}
var rowIndexesToMove = new List<int>(rowsCount);
var firstNonEmptyRowIndex = Array.FindIndex(playingGrid, row => row.Contains(true));
// save full row indexes (start from first non-empty one, no need to check top empty rows)
for (var rowIndex = firstNonEmptyRowIndex; rowIndex < playingGrid.Length; rowIndex++)
{
var row = playingGrid[rowIndex];
if (!row.Contains(false))
{
rowIndexesToMove.Add(rowIndex);
}
}
// move full rows to top
for (var fullRowIndex = 0; fullRowIndex < rowIndexesToMove.Count; fullRowIndex++)
{
for (var rowIndex = rowIndexesToMove[fullRowIndex]; rowIndex >= firstNonEmptyRowIndex + fullRowIndex; rowIndex--)
{
for (var c = 0; c < playingGrid[firstNonEmptyRowIndex + fullRowIndex].Length; c++)
{
playingGrid[rowIndex][c] = false;
}
if (rowIndex > 0)
{
var temp = playingGrid[rowIndex];
playingGrid[rowIndex] = playingGrid[rowIndex - 1];
playingGrid[rowIndex - 1] = temp;
}
}
}

Average of of 2 coordinates

So I am making a graph of the average between to lists of coordinates.
So I have been looking all over and can't seem to find any information on how I can find the average of the 2 lists of values. When I try I get an error "the index was outside the matrix boundaries" and when I got it to work I just made a graph where the years where extremely high and the graph itself were looking insane. What i do is importing 2 parts of(data/data2) information with Json.
//
// Data
//
int tal = dataSet.dataset.value.Count;
//Add items in the listview
int[] yData = new int[tal];
int[] xData = new int[tal];
int k = 0;
foreach (var item in dataSet.dataset.dimension.Tid.category.label)
{
xData[k++] = int.Parse(item.Value.ToString());
}
for (int i = 0; i < tal; i++)
{
yData[i] = dataSet.dataset.value[i];
}
//
// Data2
//
int tal2 = dataSet2.dataset.value.Count;
int[] y2Data = new int[tal2];
int[] x2Data = new int[tal2];
int j = 0;
foreach (var item in
dataSet2.dataset.dimension.Tid.category.label)
{
x2Data[j++] = int.Parse(item.Value.ToString());
}
for (int p = 0; p < tal2; p++)
{
y2Data[p] = dataSet2.dataset.value[p];
}
This is the part
///////////////////////////////////////////////////////////
int[] ySum = new int[xData.Length];
for (int i = 0; i < xData.Length; i++)
{
ySum[i] = (yData[i] + y2Data[i]) / 2;
}
///////////////////////////////////////////////////////////
List<int> GenUd = new List<int>(yData.ToList());
textBoxGenUd.Text = GenUd.Average().ToString();
List<int> GenInd = new List<int>(y2Data.ToList());
textBoxGenInd.Text = GenInd.Average().ToString();
chartArea1.Name = "ChartArea1";
chart2.ChartAreas.Add(chartArea1);
chart2.Dock = DockStyle.Fill;
for (int i = 0; i <xData.Count(); i++)
{
series1.Points.AddXY(ySum[i], x2Data[i]);
}
MySecChart2 mc3 = new MySecChart2(series1);
mc3.ShowDialog();
mine is just an educated guess - if the exception occurs at the following:
This is the part
///////////////////////////////////////////////////////////
int[] ySum = new int[xData.Length];
for (int i = 0; i < xData.Length; i++)
{
ySum[i] = (yData[i] + y2Data[i]) / 2;
}
///////////////////////////////////////////////////////////
My diagnosis would be that yData[i] and y2Data[i] they dont have the same length and of xData.Length define into the loop definition.
Was perhaps supposed to be yData.Length in the loop definition?

keeping all result in a single list in linq C#?

Using the simple example below,I aim to keep linq results within same list:
for (int j = 0; j < YatakList.Count; j++)
{
if (YatakList[j].GrupId != Op.yatakGrupId)
{
continue;
}
var AllBlanks = YatakList[j].blanks
.SelectMany((item, index) =>
item.Select(entry => new
{
Index = index,
Start = entry.Key,
Length = entry.Value,
Id = YatakList[j].Id,
GrupId = YatakList[j].GrupId
}))
.OrderBy(item => item.Start);
var LenghtSuitingBlanks = from blank in AllBlanks
where (blank.Length >= Op.sure)
select blank;
var closestDiff = LenghtSuitingBlanks.First();
}
By using for loop I get first result from YatakList,but within second case (j=1) of for loop altought I get the second result set I am loosing the first one.
My Question is :
How can I keep the result set of linq in a list
OR
Is there a alternative way (preferred solution) that does not use for loop
I solved the problem by crating an class named UygunYatak then by the help of another loop collect all elements in a List.
for (int i = 0; i < YatakList.Count; i++)
{
if (YatakList[i].GrupId != Op.yatakGrupId)
{
continue;
}
var AllBlanks = YatakList[i].blanks.SelectMany((item, index) => item.Select(entry => new { Index = index, Start = entry.Key, Length = entry.Value, Id = YatakList[i].Id, GrupId = YatakList[i].GrupId })).OrderBy(item => item.Start);
var LenghtSuitingBlanks = from blank in AllBlanks where (blank.Length >= Op.sure) orderby blank.Start select blank;
for (int j = 0; j < LenghtSuitingBlanks.Count(); j++)
{
UygunYatak uygunYatak = new UygunYatak();
uygunYatak.Id = LenghtSuitingBlanks.ElementAt(j).Id;
uygunYatak.GrupId = LenghtSuitingBlanks.ElementAt(j).GrupId;
uygunYatak.Start = LenghtSuitingBlanks.ElementAt(j).Start;
uygunYatak.Length = LenghtSuitingBlanks.ElementAt(j).Length;
UygunYatakList.Add(uygunYatak);
}
}
public class UygunYatak
{
public int Id;
public int GrupId;
public int Start;
public int Length;
}

storage selected listbox items into a new array

How can I store the selected item(s) in a listbox to a new array? Like we select the items and then do the button's action, e.g.
string[] domains = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
domains[i] = listBox1.SelectedIndices[i].ToString();
}
Your code won't work because index i is not used to indicate SelectedIndices, but Items.
So update it:
string[] domains = new string[listBox1.SelectedIndices.Count];
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
domains[i] = listBox.Items[listBox1.SelectedIndices[i]].ToString();
}
What I prefer:
List<string> domains = new List<string>();
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
domains.Add(listBox.Items[listBox1.SelectedIndices[i]].ToString());
}
What about this:
string[] domains = listBox1.SelectedItems.OfType<string>().ToArray();

Categories