deleting from a list in c# - c#

I have a wpf c# application, that loads tasks to a treeView from a text file, the data about the tasks are loading into a list, Im trying to delete data in position I in the list, but i can not figure out how. I have this for loop checking to see if the selected treeView item is equal to the item in position I in the list and if so I wanna delete that item from the list. Here is the for loop which works, im just wondering how to do the actual delete, I've tried things such as .delete and .remove which I found on msdna.
for (int i = 0; i < name.Count; ++i)
{
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
if (selectName == name[i])
{
//name.Remove(i) or name.Remove[i] or name[i].Remove
}
}

If name is a List<T> then you probably want name.RemoveAt(i) if I understand the question correctly.
Alternatively, you could just use name.RemoveAll(n => n == selectName); instead of the for loop.

How about:
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
name.RemoveAll(x => x == selectedName);

You have to give List.Remove() an object, not an index.
So, assuming name is a List,
name.Remove((TreeViewItem)treeView1.SelectedItem);

Depending on your application needs, your data structure should have some time of Parent-Child relationship. All you should have to do is remove the Child that is selected from the parent, and the TreeView in WPF should just automatically update.
I suggest you read Josh Smith's article on using ViewModels with the TreeView for easier management. Trust me it will make your life a lot easier the sooner you start using a stronger data structure, and just remember that a TreeView only represents the data provided, and you should have other methods to edit the data directly and not the TreeView.

Related

CosmosDB SQL API: Change property Name - (Partial Updates .Net SDK or loops)

Consider the following data
I am trying to update the property name from givenName to PetName. Below are the things I tried.
Option 1:
List<PatchOperation> patchOperations = new List<PatchOperation>();
dynamic data = new
{
PetName= "Dummy"
};
//Remove the 0th element from the pet array
patchOperations.Add(PatchOperation.Remove("/children/0/pets/0"));
//insert the new entity
patchOperations.Add(PatchOperation.Add<dynamic>("/children/0/pets/0", data));
await Container.PatchItemAsync<dynamic>("AndersenFamily", new
PartitionKey("AndersenFamily"), patchOperations);
This works on the first element (as I have specified the index as 0).
But I couldn't find a way to update all the elements in the array. Something like /children/*/pets (I get an error "Invalid navigation for array(*)").
Option 2:
Read all the data from the database, remove the existing pets and upload the new object with the property PetName. But I guess it will take lot of time to loop through all the objects and make these changes.
Can someone suggest a better solution or a way to fix the partial updates so that it updates all the elements in the array?
The actual dataset that I am working on is huge and has more nested arrays.
Yes you are right, Current version of Patch does not support patching nested array. You need to know the index in order to replace/add values as you mentioned in the first question.
One way to think about this would be to write your own logic to replace the keys , there are libraries available in dotnet and then using the bulk insert using the SDK to insert those documents.

Slow iterating thru Outlook.Items to find contacts

I am having an issue where I am trying to find at least one contact inside any of the outlook folders. I have a recursive function that iterates thru the items inside a folder and if the item is of type contact then we add it to a list. However, this code runs extremely slow when folders have a large number of records say 4000 items.
Is there any way just to get contacts or is there a way to make this code more efficient?
foreach (var item in folderBase.Items)
{
if (returnFirst && result.Count > 0)
break;
if ((item is Outlook.ContactItem))
{
result.Add((Outlook.ContactItem)item);
}
}
Firstly, storing 4000 live Outlook objects in a list is a bad idea: you will run out of RPC channels in case of online Exchange store at item 255. Store the entry ids and use them to call Namespace.GetItemFromID() when you actuqlly need it; then release it as soon as you are done.
Secondly, use MAPIFolder.GetTable - it will let you retrieve values from multiple items without actually opening them; perfect in your case. Try something like the following (VB script):
set Folder = Application.ActiveExplorer.CurrentFolder
set Table = Folder.GetTable("[MessageClass] = 'IPM.Contact' ")
Table.Columns.Add("EntryID")
while not Table.EndOfTable
set Row = Table.GetNextRow()
vEntryId = Row.Item(1)
Debug.Print vEntryId
wend
You need to use the Find/FindNext or Restrict methods of the Items class instead.
Read more about these methods and see the sample code in the following articles:
How To: Use Restrict method in Outlook to get calendar items
How To: Retrieve Outlook calendar items using Find and FindNext methods
You can use the MessageClass property of Outlook items to get the contact items only.

Getting Value at Selected Indices in C# ASP.net ListBox

I am trying to get the value at selected indicies in a ListBox using ASP.net C#
MakeListBox.SelectionMode = ListSelectionMode.Multiple;
int [] indicies= MakeListBox.GetSelectedIndices();
I am going to dynamically building a select statement for a database query to an SQLDataSource. What I had hoped to be able to do was get all the selected indexes go through a loop to for the array that it returns and add each value at the specified index to a string.
I have looked through this and this but can't find what is needed to do what I have talked about.
Basically I am looking for the opposite of the IndexOf command. Or a technique that would have the same results.
It's not clearly obvious when looking at the ListBox control properties, but this is the best way to do it:
foreach(ListItem li in MakeListBox.Items)
{
if(li.Selected)
{
// Append to your string list
}
}

Article/news ordering by order id

I m having a little trouble coming up with a schema to order and changing order in a article/news management system.
Here goes:
I have News Object Model as follow:
class News {
int id;
string Title;
string Content;
string OrderId;
// trimmed
}
I have CRUD for the object model. and List as follows:
Id Title Order
1. Foo -+
2. Bar -+
3. Glah -+
What i want to do is when user clicks on - for first news, i want to replace 1 and 2 orderid and of course display as well.
well how do i do this on server side? lets say for 1. item order id is 1 , how do i find the first item that has a higher order id then this one?
Or take 2. Bar, when i click on - , how do i find/replace order ids with first one. or i click on + how do i replace order id of this news with 3. Glah ?
is there a better way of doing this?
There are also some UI where user drags and drops ? any pointers on that?
When the user changes the location of an item, the server needs to know which item was changed and what it's new position is. In the code below, I'm using a List to figure out the new positons. In this sample code, newPosition is the new zero-based position and selectedArticle is the article that was moved.
List<Article> articles = LoadSortedArticles()
articles.Remove(selectedArticle);
articles.Insert(newPosition, selectedArticle);
UpdateArticles(articles);
After running, the article's index within the list tells you its new position. That applies to all articles in the list and works the same for a drag/drop UI. I don't know entity framework, but if you can map the orderId field in the database to the index of the article in the list, then you should be good to go.
I hope this is at least able to give you some ideas. Maybe someone else can give a solution specific to entity framework, but I think putting the articles into a sorted list and letting the list do the work might be the easiest way.
I think the best approach here would be to implement a Swap method that takes two News objects and swaps their OrderId, which I am assuming is numeric.
So the idea would be that you would pass this method the object that was clicked on, as well as either the one above (if the user clicked +) or the one below (if they clicked -). If you do it this way then you avoid the task of trying to find the next or previous item.
In the case of drag and drop, the task is a little different. You would first need to retrieve all the items between (and including) the item being dragged and the drop target, ordered by OrderId. From there you swap the dragged item with the next or previous item (depending on direction), and continue doing so until you have swapped it with the drop target.

How to databind a textbox to a DB field using LINQ to SQL, the right way?

Currently I'm writing an ASP.net webforms app, which works with a table containing 15 fields.
I'm using LINQ to SQL which is very good to use and everything, but when i need to bind the fields with the text boxes (15 of em), whats the best way currently I'm doing this
...
...
var stud = db.Students.SingleOrdefault(d => d.ApplicationNo == 32)
textbox1.Text = stud.Name;
...
...
I feel like I'm "missing the point". Please advise on the best way to do this.
Also what should i do to implement
the Move next and move previous
functionality like a recordset
i mean how can i move to the next/previous "student" in db.Students collection.
You can bind to an ASP.NET DetailsView control; this control supports displaying one record at a time, and can page through the results, so you don't have to do all that extra work. You could also consider a FormView too, but you have to control that.
I think you have the right approach; you could also use ElementAt() to retrieve by an index; that can have some performance implications though.
HTH.
You can use the Skip and Take method.
Update after comment:
You don't have to iterate through the different properties of the retrieved Student object. They're simply there. So you could use it like this:
txtName.Text = stud.Name;
txtAge.Text = stud.Age.ToString();
...
If you want to iterate to another record you can use the Skip and Take methods.
Grz, Kris.

Categories