last day (15 days later) » 

10:27 PM
1
A: Lambda expression (or other way) to search collection

Modar Navar MRecords = myList.Where(x=>x.toUpper().Contains("M")); var result = new HashSet<"yourClass">(); //i didn`t use hashset before but this as stated in [here](http://stackoverflow.com/questions/6391738/what-is-the-difference-between-hashsett-and-listt) eliminates duplicates foreach(var record ...

 
Thanks but how will this include parent that has no "M" if its child has "M". It looks like this only includes all itesm (parents and childrens) with "M" in them because that is what MRecords will contain
 
sorry i was fast typing because of community xD ,, return the result not the MRecords
the MRecords is a helper list used to get all records having M .. then loop on Mrecords ,, add them with their parents to the final result ( which is a HashSet eliminating the duplicates
 
hmmm, although I can understand your solution best, I dont seem to be able to use it. The problem is that in your solution record would be my BodyTreeNode object but the ParentLooper.parent is my BodyNode object. BodyTreeNode descends from BodyNode but I am unable to do the 2 lines inside your while loop.
 
can you add the structure of BodyTreeNode and BodyNode
 
10:27 PM
updated, much appreciated
 
hello
i see BodyNode parent is a body node too
 
first time using chat
thanks for all the help
 
np
does this var myList = myCollection.Values.ToList(); contains all your planets moons atoms etc...
 
yes
 
what type is var
myList
 
10:34 PM
List<BodyTreeNode>
 
bodyTreeNode is the child class of bodyNode
so you can access the parent property , am i right
 
no, not from myList
 
from myList.first ?
 
it would have to be myList[0].Parent
 
ohh
 
10:40 PM
sorry, my bet
I can but it is list so have to index it
 
the idea is
result in my example
i didn`t sepcify the type
so you specify it as BodyNode
and at this line result.Add(record);
replace it by result.Add((BodyNode)record);
i`m not 100 sure that you can downcast this like this
or another way
 
I tried this. I will post what I have, give me a min
    public List<ChoiceNode> FilterTree(List<T> list)
    {
        var records = list.Where(x => x.Name.ToUpper().Contains("M"));
        var set = new HashSet<ChoiceNode>();
        foreach (var rec in records)
        {
            ChoiceNode node = rec;
            ChoiceNode parent = node.Parent;
            set.Add(node);
            while (parent != null)
            {
                set.Add(parent);
                parent = parent.Parent;
            }
        }
        var result = set.ToList();
the naming is bit different, just use Choice instead of Body but that is what it is
The problem is that I now passed in a List<T> which is my List<ChoiceTreeNode> but I am returning List<ChoiceNode>. I would like to actually return List<ChoiceTreeNode>
so that is what I have so far and the problem I am facing
 
hummm
let`s try not downcasting then
replace every ChoiceNode with ChoiceTreeNode
 
I am trying to call this like below
 
it shouldn`t be a problem because ChoiceTreeNode has the parent property
plus
 
10:48 PM
I create myList<ChoiceTreeNode>to use it for tree building. But if I happen to search for say all nodes (or they parents) containing "m" then I am trying to modify myList to include only nodes with "m" and their parents (regardless if parent has "m" or not). So, I am trying just to pass myList and get new back
 
as a start .. specify the type of the list in the function header
 
var myList = myCollection.Values.ToList();
 
after making it work you can get it back to T
 
if searching
myList = FilterTree(myList)
 
try this
 
10:51 PM
this is what I am trying to do:
var myList = myCollection.Values.ToList();

if (searching)
{
myList = FilterTree(myList);
}

BuildTree(); // build tree using myList
 
public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
{
var records = list.Where(x => x.Name.ToUpper().Contains("M"));
var set = new HashSet<ChoiceTreeNode>();
foreach (var rec in records)
{
ChoiceTreeNode node = rec;
ChoiceTreeNode parent = node.Parent;
set.Add(node);
while (parent != null)
{
set.Add(parent);
parent = parent.Parent;
}
}
var result = set.ToList();
return result;
}
 
Let me try, I think that should work
sorry, new to generics and .net
 
it is not your fault i crave for precious stack points :D
 
so same issue I had when I tried it on this line
ChoiceTreeNode parent = node.Parent;
 
what`s the exception again
 
10:55 PM
Error 13 Cannot implicitly convert type 'ChoiceNode' to 'ChoiceTreeNode'. An explicit conversion exists (are you missing a cast?)
 
ohh right
 
so I guess casting
 
wait i got a solution
but not so professinal
 
after casting I now have error Error 16 The best overloaded method match for 'ChoiceList<T>.FilterTree(System.Collections.Generic.List<ChoiceTreeNode>)' has some invalid arguments
on call to my method
 
parent = list.firstorDefault(x=>x.Id == parent.Parent.Id);
with this line we are looping on originial list to get the parent using Id
 
10:59 PM
trying to understand
 
parent.Parent is choice node containing Id
list is a <choice tree node > list
my fault
one second
 
I guess you mean
ChoiceTreeNode parent = list.FirstOrDefault(x => x.ID == x.Parent.ID);
 
replace the error line with ChoiceTreeNode parent = list.firstorDefault(x=>x.Id == node.Parent.Id);
no no
don`t use x twice inside
i`ll repost the func
 
btw, ChoiceNode.ID is not same as ChoiceTreeNode.ID
 
public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
{
var records = list.Where(x => x.Name.ToUpper().Contains("M"));
var set = new HashSet<ChoiceTreeNode>();
foreach (var rec in records)
{
ChoiceTreeNode node = rec;
ChoiceTreeNode parent = list.firstordefault(x=>x.id== node.Parent.id);
set.Add(node);
while (parent != null)
{
set.Add(parent);
parent = parent.Parent;
}
}
var result = set.ToList();
return result;
}
that`s the catch ;)
now the error is in the while loop right
 
11:04 PM
I am not sure this line will work
ChoiceTreeNode parent = list.FirstOrDefault(x => x.ID == node.Parent.ID);
as the ChoiceNode.ID is not same as ChoiceTreeNode.ID
but will try
 
you`ll get an error by inside the while now
public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
{
var records = list.Where(x => x.Name.ToUpper().Contains("M"));
var set = new HashSet<ChoiceTreeNode>();
foreach (var rec in records)
{
ChoiceTreeNode node = rec;
ChoiceTreeNode parent = list.firstordefault(x=>x.id== node.Parent.id);
set.Add(node);
while (parent != null)
{
set.Add(parent);
parent = list.firstordefault(x=>x.id== parent.Parent.id);
}
}
var result = set.ToList();
return result;
}
worked ?
shit
 
but right now, my bigger problem is that I error on call to this method
myList = FilterTree(myList);

I get error
Error 16 The best overloaded method match for 'ChoiceList<T>.FilterTree(System.Collections.Generic.List<ChoiceTreeNode>)' has some invalid arguments
and this
Error 17 Argument 1: cannot convert from 'System.Collections.Generic.List<T>' to 'System.Collections.Generic.List<ChoiceTreeNode>'
 
how it is returning T
 
I have no idea, this is what is confusing me from the very beginning
 
btw if id is diffrent
use name instead
 
11:12 PM
no, the Name is different, too. Once is Jupiter, other is Ganymede for example
but for now, why am I getting errors on call to FilterTree()?
 
You're using visual studio right
There cant be 2 errors
One at a time
Whats the current
Are u still here
 
unfortunatelly I cannot post image
but there are 2 errors
 
not at run time you mean
ahh
 
Error 16 and 17 I posted above, both point to same like of code which is my call to FilterTree()
debug time
sorry, compile time
I am not able to compile
 
are you using this ??
public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
{
var records = list.Where(x => x.Name.ToUpper().Contains("M"));
var set = new HashSet<ChoiceTreeNode>();
foreach (var rec in records)
{
ChoiceTreeNode node = rec;
ChoiceTreeNode parent = list.firstordefault(x=>x.id== node.Parent.id);
set.Add(node);
while (parent != null)
{
set.Add(parent);
parent = list.firstordefault(x=>x.id== parent.Parent.id);
}
}
var result = set.ToList();
return result;
}
and building the project which has this function
 
11:21 PM
yes
 
if you hover mouse at
var myList = myCollection.Values.ToList();
var
what is the hovered
 
have to comment the call in order to run and hover, give me sec
it gives me a list of ChoiceTreeNode elements. myList type is List<ChoiceTreeNode>
 
:O
 
That is what I get when I gover over myList
 
isn`t this the same
as the types used in the function
 
11:27 PM
so it is System.Collections.Generic.List<ChoiceTreeNode>
 
public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
 
it is
 
ahh
you didn`t replace the T
This errors are beyond my powers
 
I dont see any T, here is my method

public List<ChoiceTreeNode> FilterTree(List<ChoiceTreeNode> list)
{
var records = list.Where(x => x.Name.ToUpper().Contains("M"));
var set = new HashSet<ChoiceTreeNode>();
foreach (var rec in records)
{
ChoiceTreeNode node = rec;
///ChoiceTreeNode parent = (ChoiceTreeNode)node.Parent;
ChoiceTreeNode parent = list.FirstOrDefault(x => x.ID == node.Parent.ID);
set.Add(node);
while (parent != null)
{
set.Add(parent);
//parent = (ChoiceTreeNode)parent.Parent;
parent = list.FirstOrDefault(x => x.ID == parent.Parent.ID);
 
Thats flawless
This discussion has taken a while
I consumed all my brain
 
11:32 PM
Thanks for your help, I'll continue trying to figure out
Much appreciated Modar
:)
 
Best of luck
Picture cell
 
thanks man
 
11:45 PM
If I am howering over myList, it actually shows List<T>
 

last day (15 days later) »