Code:
foreach (var item in items.Children)
{
RadTreeViewItem parent1 = new RadTreeViewItem();
parent1.Header = NodeHeader(item.Path, item.Name, SelectedPath, ProjectData);
parent1.Tag = item;
foreach (var child in item.Children)
{
RadTreeViewItem children = new RadTreeViewItem();
children.Header = NodeHeader(child.Path, child.Name, SelectedPath, ProjectData);
children.Tag = child;
parent1.Items.Add(children);
}
Parent.Items.Add(parent1);
}
items.Children and item.Children are ObservableCollection<>
parent1.Header and children.Header are HeaderedItemsControl.Header
parent1.Tag and children.Tag are FrameworkElement.Tag
How to convert the above foreach nested loop to LINQ ?
LINQ... Language INtegrated (and here's the key bit) Query.
What you are doing is not a query. Leave it with the foreach loops; it is fine, it is clear, it is obvious.
In particular, the .Items.Add methods on the various collections is not really something you can trivially reproduce in LINQ. You can probably do it, but it will be ugly and hard to maintain. What you have is fine. If you want to change it for change's sake, maybe:
RadTreeViewItem parent1 = new RadTreeViewItem {
Header = NodeHeader(item.Path, item.Name, SelectedPath, ProjectData),
Tag = item
};
foreach (var child in item.Children)
{
parent1.Items.Add(new RadTreeViewItem {
Header = NodeHeader(child.Path, child.Name, SelectedPath, ProjectData),
Tag = child
});
}
Parent.Items.Add(parent1);
Not exactly an improvement, IMO.
Related
I have the following code where I am checking if some elements are not matching in my dictionary then I want to remove the unmatching elements from the local item. The problem is, When a value is removed from the collection, for some reason it also modifies the parental structure.
My other problem is, for example if I have list as "A","B","B", using the Except is only giving me the single B but not the other. Please help.
public void AddLogs(IEnumerable<ReportGenerationTypes> subElements)
{
var changeDetails = new Dictionary<AuditSaveHeader, List<string>>();
List<string> AuditableItems = null;
List<string> subItems = new List<string>();
foreach (var item in subElements)
{
subItems.Add(item.ToString());
}
foreach (var item in auditLogData?.AuditHeaders)
{
if (!changeDetails.ContainsKey(item))
{
changeDetails.Add(item, null);
}
AuditableItems = new List<string>();
foreach (var inner in item.AuditChangeValues)
{
AuditableItems.Add(inner.Auditable.ToString());
}
changeDetails[item] = AuditableItems;
}
for (int i = 0; i < changeDetails.Count; i++)
{
var result = kp.Value.Except(subItems);
Auditable AuditItem = Auditable.Unassigned;
//I think the problem lies with the below code not sure.
if (result != null && result.Count() > 0)
{
foreach (var item in result)
{
Enum.TryParse(item, out AuditItem);
var itemToRemove = kp.Key.AuditChangeValues.Where(x => x.Auditable == AuditItem).FirstOrDefault();
//The following line effects the AuditChangeValues object and not just my dictionary.
kp.Key.AuditChangeValues.Remove(itemToRemove);
}
}
}
}
Promoting my comment to answer:
You are using some vars that are not shown, like kp, auditLogData, etc. and overall is not clear what you want to achieve.
Anyway I agree the problem is you are editing the reference to an object. You could try cloning the objects, etc. But without really understanding the code is hard to tell.
I`m trying to make a query in robomongo but there seems to be too many results in the query.
List<DataObject> list = collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")).ToList();
foreach (DataObject item in list)
{
//Do Something
}
From what i read, i would need to use MongoCursor, but I couldnt find a good example, is there iterate through everything using batches of 1000?
Something like:
MongoCursor<DataObject> cursor = collection.FindAs<DataObject>(Query.EQ("Item1", "Value1"));
int batchNumber = 1000;
List<DataObject> list;
while(list = cursor.getBatch(batchNumber);)
{
foreach (DataObject item in list)
{
//Do Something
}
}
Now I understood i could easily solve this if i dont save it in a list before the foreach by doing :
foreach (DataObject item in collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")))
{
//Do Something
}
This was solved by not saving the result in a list before the foreach. Like this
foreach (DataObject item in collection.FindAs<DataObject>(Query.EQ("Item1", "Value1")))
{
//Do Something
}
I want to create a hierarchical view of strings based on first two characters.
If the strings are:
AAAA,AAAA,BBDD,AABB,AACC,BBDD,BBEE
I want to reate a treeview that looks like this:
AA
AAAA
AABB
AACC
BB
BBDD
BBEE
I currently have some code that looks like this (inside a loop over the strings):
TreeNode pfxNode;
if (treeView1.Nodes[pfx]!=null) {
pfxNode = treeView1.Nodes[pfx];
}
else {
pfxNode = treeView1.Nodes.Add(pfx);
}
if (!pfxNode.Nodes.ContainsKey(string)) {
pfxNode.Nodes.Add(string, string + " some info");
}
For some reason this ends up with multiple "AA" nodes at the top level.
What am I missing?
please no pre-filtering of strings I want to be able to check if a specific treenode exists based on its key.
thanks
else {
pfxNode = treeView1.Nodes.Add(pfx);
}
There's your mistake, you are forgetting to set the key of the tree node. So the next ContainsKey() won't find it. Fix:
pfxNode = treeView1.Nodes.Add(pfx, pfx);
Use this:
var q = from s in arr
group s by s.Substring(0, 2) into g
select new
{
Parent = g.Key,
Children = g.Select (x => x).Distinct()
};
foreach (var item in q)
{
var p = new TreeNode(item.Parent);
TreeView1.Nodes.Add(p);
foreach (var item2 in item.Children)
p.Nodes.Add(new TreeNode(item2));
}
This is embedded in another loop, and well, it's pretty slow. Is there a better way to do this?
for(int i=0;i< listView.Items.Count;i++)
{
if(listView.Items[i].SubItems[3].Text == "asdf")
{
}
}
Well there's a nicer way to do it:
foreach (ListViewItem item in listView.Items)
{
if (item.SubItems[3].Text == "asdf")
{
...
}
}
Or you could use LINQ:
var query = listView.Items
.Cast<ListViewItem>()
.Where(item => item.SubItems[3].Text == "asdf");
foreach (var item in query)
{
...
}
I doubt that that will be faster though...
Does your outer loop change the listView? If not, could you do the query once and reuse the results in the outer loop?
In case someone runs across this using WPF, you don't get .SubItems on item when you use foreach (ListViewItem item in listView.Items). Instead, I found I could just use DataRowView and get the value of the cell that way:
foreach (DataRowView drv in listView.Items)
{
if (drv.Row[3].ToString() == "asdf")
{
...
}
}
You do have to add a using System.Data; statement at the top of your class to use it. I found it worked in WPF, and it might in other areas (i.e. WinForms), as well.
I have a TreeView control that I have created in XAML in a WPF program
After adding a couple nodes at the root level, I have written code that loops through the tree structure like so:
ItemCollection items = treeView1.Items;
foreach (TreeViewItem n in items)
{
...
}
Once I find the place in this loop where I want to include a child node, how do I go about inserting a child?
This is a piece of very naive code that does it, you might want to make it more defensive if you actually use it.
var items = treeView1.Items;
var item = new TreeViewItem() { Header = "Interesting" };
items.Add(item);
var subitem = new TreeViewItem() {Header = "Sub Item"};
foreach (TreeViewItem n in items)
{
if (n.Header == "Interesting")
(n as TreeViewItem).Items.Add(subitem);
}