C# Winform (Entity Framework) - Iterate thru DBLocal - c#

I am working with a project modeled on this link: Databinding with WinForms
On the form load on the link, the tutorial set the binding to:
this.categoryBindingSource.DataSource =
_context.Categories.Local.ToBindingList();
my question is, is it possible to iterate rows and columns on DBlocal? _context.Categories.Local? If Yes, how?
Thanks in Advance.

DbSet<T>.Local property is ObservableCollection<T> and you can simply use a for/foreach loop. You can use either of these options:
foreach (Category item in context.Category.Local)
{
//MessageBox.Show(item.Name);
}
for (int i = 0; i < context.Category.Local.Count; i++)
{
var item = context.Category.Local[i];
//MessageBox.Show(item.Name);
}
context.Category.Local.ToList()
.ForEach(item =>
{
//MessageBox.Show(item.Name);
});
Don't forget to first load data to Local.

Related

Datagridview - Quicker way to populate with List loop?

Currently, I'm planning to add ~500K List to datagridview.
Class has multiple Fields, Each List has 13 fields.
but i bind only 7 fields for now.
Problem is,it seems like adding takes too much time. like 5sec for 15K which is awful.
Is there any ways to optimze this?
or should i throw away datagridview and consider some other views?
private void UpdateDataGrid()
{
this.dataGridView1.Rows.Clear();
for (int i = 0; i < gVar.gTagCount; i++)
{
this.dataGridView1.Rows.Add(new object[]
{
gVar.Tags[i].TagCount,
gVar.Tags[i].Name,
gVar.Tags[i].Score.Story,
gVar.Tags[i].Score.Drawing,
gVar.Tags[i].Score.Drawing,
gVar.Tags[i].Score.Memetic,
gVar.Tags[i].DupeCount
});
}
}
According to what we discussed my approach would be this:
First make a new Class, I would call it TagsMin this class should contain the 7 things you want in your datagridview.
Secondly i would populate that a list of that class like this (you need to complete it with what you want):
var tagList = gVar.Tags.Select(x => new TagsMin() { TagCount = x.TagCount, Name = x.Name... }).ToList()
And the last step, would be to bind it to the datagridview:
dataGridView1.DataSource = tagList;
Consider using paging so that you are not loading all of the data at once. The answer to the question linked to below provides an example.
How can we do pagination in datagridview in winform
Can you try to avoid the loop and directly bind the list with the standard way:
dataGridView1.DataSource = null;
dataGridView1.DataSource = gVar.Tags;

Matching lisboxes items and creating result

I am creating an Exam system in c#. I am creating result, i have answers in a listbox1 and correct answers in another listbox2, my issue is values in the listboxes should be compared and result should be generated on its base. If half the values match student is pass otherwise fail.
My code for this is following but it does not work.
for(int intCount = 0; intCount < listBoxSanswers.Items.Count;intCount++)
{
for (int intSubCount = 0; intSubCount < listBoxActAnswers.Items.Count; intSubCount++)
{
if (listBoxActAnswers.Items[intCount].ToString() == listBoxActAnswers.Items[intSubCount].ToString())
{
listBox3.Items.Add(listBoxActAnswers.Items[intCount].ToString());
}
}
}
If you want to use your approach, than you have to change one of the two lists to listBoxSanswers
If you want a shorter way, without the loops, you can try this line:
listBox3.Items.AddRange(listBoxActAnswers.Items.Cast<string>().ToList().Intersect(listBoxSanswers.Items.Cast<string>().ToList()).ToArray());
EDIT:
Oh okay, so you have a DataTable as a DataSource.
Than you can do it this way:
listBox3.Items.AddRange(listBoxActAnswers.Items.Cast<DataRowView>().Select(r => r[0]).ToList().Intersect(listBoxSanswers.Items.Cast<DataRowView>().Select(r => r[0]).ToList()).ToArray());
Maybe you should adapt Select(r => r[0]) to the right column which is your DisplayMember.

c# collections and re-numbering not working as expected

Hi i'm trying to setup simple test data.
I simply want to take a collection which is smallish and make it bigger by add itself multiple times.
After I;ve added them together i want to re-number the property LineNumber
so that there are no duplicates and that it goes in order. 1,2,3,4....
no matter what i try it doesn't seem to work and i cant see the mistake.
var sampleTemplateLine = dataContext.TemplateFileLines.ToList();
*//tired this doesnt work either*
//List<TemplateFileLine> lineRange = new List<TemplateFileLine>();
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);
var allProducts = sampleTemplateLine
.Concat(sampleTemplateLine)
.Concat(sampleTemplateLine)
.Concat(sampleTemplateLine)
.ToList();
int i = 1;
foreach (var item in allProducts)
{
item.LineNumber = i;
i++;
}
this doesnt seem to work either
//re-number the line number
var total = allProducts.Count();
for (int i =0; i < total; i++)
{
allProducts[i].LineNumber = i+1;
}
PROBLEM: below RETURN 4 when i'm expecting 1
var itemthing = allProducts.Where(x => x.LineNumber == 17312).ToList();
You are adding the same objects multiple times. You wold have to add new objects or clone the ones you have.
The problem is they are pointing the same object. So if you change a property it changes all the pointed objects at the same
You can use Clone method if it exist, if not you can create your own Clone method like in this question.

Set table values equal to value in other table

I have one table which functions as a viewModel. From this, it is supposed to be the source of my binding in my view. My object is to update this table/viewModel based on two other tables. I have one property from each of the table checklistTable and AnswerTable that is to be set in my viewModel checkViewModel.
At the moment I am querying the current elements I need from each of the tables, and trying to update the viewModel.
CodeBehind:
//Curent descriptions to be set in the viewModel
var currentDescription = (_check.Where(s => currentElements.Contains(s.defineId)).Select(s=> s.Description).ToList());
//Current colors to be set in the viewModel
var currentColors = (from current in _answer
where current.questionId == currentCheckId &&
current.buildingId == currentBuildingId
orderby current.dateReported descending
select current.backgroundColor).ToList();
After retrieving theese values, i try to update my viewModel, and this is where things go wrong:
for (int i =0 ; i < currentDescription.Count() - 1; i++)
{
currentViewTable.Description = currentDescription[i];
}
for (int i =0 ; i < currentColors.Count() - 1; i++)
{
currentViewTable.backgroundColor.Insert(i,currentColors[i]);
}
I get the error: reference not set to an instance of an object. Are there better ways to update my viewModel, of or any tips on what I am doing wrong?
I would first recommend to use a foreach loop instead of a regular for loop. I find it much easier to use foreach when working with lists, this will eliminate any clear "off by one" errors usually.
foreach (var description in currentDescription)
{
//do something given each element in the list
}
This seems to be a data integrity issue from what I can gather just looking at your code and given what the error is stating. I would recommend going into debug mode and looking in the Lists that you have generated to see if there are any indexes where the objects in your two lists are null, and handle these instances accordingly.

Retrieve Value of DropDownList by Index ID

I am trying to retrieve the value of a dropdown list by specifying its indexid but I cant seem to find a way to accomplish this. I dont want it to be the selected value either, basically I need to go through the list, and then add it to an array. I can find all kinds of ways to get it based on the value but not by the index id anyone know how to do this? Im using c#.
string valueAtIndex = myComboBox.Items[SomeIndexValue].Value;
Have you tried lstWhatever.SelectedIndex?
Since the object supports IEnumerable, you can use foreach to loop through, if that is your intention.
Is this what you are looking for?
List<String> theList = new List<string>();
foreach (var item in comboBox1.Items)
{
theList.Add(item.ToString());
}
Where comboBox1 is your dropdown list and i assumed you are talking about a windows forms project.
Or, if you want to us Index Id:
List<string> theList = new List<string>();
for (int i = 0; i < comboBox1.Items.Count; i++)
{
theList.Add(comboBox1.Items[i].ToString());
}
String valueAtIndex = myComboBox.Items[myComboBox.SelectedIndex].ToString();

Categories