2 List<T> collections help

R

rodchar

hey all,

i have 2 List<T> collections as noted in the subject (call it ListA and
ListB).
i need to iterate thru ListA and for each object in the collection i need to
find one of the columns in ListA inside ListB, and if it's found return that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the syntax.

thanks,
rodchar
 
B

b

Rodchar,

You would iterate through A using a foreach

YourType retrievedItem;

foreach(YourType item in A)
{
retrievedItem = B.Find(A.compareItem);
}

If you're planning on keeping a group of them, then you'll need to set the
variable as a List<T>, also, and add them to the list.
 
B

b

Actually, I believe that this is the syntax that you would need to use...

YourType retrievedItem;

foreach(YourType item in A)
{
retrievedItem = B.Find(x => x.compareItem == a.compareItem);
}
 
B

b

rodchar,

Sorry, but I'm probably too busy to be answering this questions so quickly.
Here it is with supporting code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Program
{
private static List<Person> listA = new List<Person>();
private static List<Person> listB = new List<Person>();

static void Main(string[] args)
{
listA.Add(new Person("Steve", "Jobs"));
listA.Add(new Person("Bill", "Gates"));
listA.Add(new Person("Dean", "Lee"));
listA.Add(new Person("Jeff", "Abernathy"));

listB.Add(new Person("Steve", "James"));
listB.Add(new Person("Bill", "Rose"));
listB.Add(new Person("Jeff", "Abernathy"));

foreach(Person person in listA)
{
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

if(tempPerson
!= null)
{
Console.WriteLine(tempPerson.FirstName);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Person
{
private string lastName = string.Empty;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

private string firstName = string.Empty;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}

public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
}
 
R

rodchar

Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

what is x and how is it defined? i'm on fx2.0.

b said:
rodchar,

Sorry, but I'm probably too busy to be answering this questions so quickly.
Here it is with supporting code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Program
{
private static List<Person> listA = new List<Person>();
private static List<Person> listB = new List<Person>();

static void Main(string[] args)
{
listA.Add(new Person("Steve", "Jobs"));
listA.Add(new Person("Bill", "Gates"));
listA.Add(new Person("Dean", "Lee"));
listA.Add(new Person("Jeff", "Abernathy"));

listB.Add(new Person("Steve", "James"));
listB.Add(new Person("Bill", "Rose"));
listB.Add(new Person("Jeff", "Abernathy"));

foreach(Person person in listA)
{
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

if(tempPerson
!= null)
{
Console.WriteLine(tempPerson.FirstName);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Person
{
private string lastName = string.Empty;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

private string firstName = string.Empty;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}

public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
}

rodchar said:
hey all,

i have 2 List<T> collections as noted in the subject (call it ListA and
ListB).
i need to iterate thru ListA and for each object in the collection i need
to
find one of the columns in ListA inside ListB, and if it's found return
that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the syntax.

thanks,
rodchar
 
B

b

Ahh...That's a lamda expression. It's not available to you. You'll have to
create a delegate, instead.

Replace "Person tempPerson = listB.Find(x => x.FirstName ==
person.FirstName)"

with

"Person tempPerson = listB.Find(delegate(Person p) {return p.FirstName ==
person.FirstName; })"

rodchar said:
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

what is x and how is it defined? i'm on fx2.0.

b said:
rodchar,

Sorry, but I'm probably too busy to be answering this questions so
quickly.
Here it is with supporting code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Program
{
private static List<Person> listA = new List<Person>();
private static List<Person> listB = new List<Person>();

static void Main(string[] args)
{
listA.Add(new Person("Steve", "Jobs"));
listA.Add(new Person("Bill", "Gates"));
listA.Add(new Person("Dean", "Lee"));
listA.Add(new Person("Jeff", "Abernathy"));

listB.Add(new Person("Steve", "James"));
listB.Add(new Person("Bill", "Rose"));
listB.Add(new Person("Jeff", "Abernathy"));

foreach(Person person in listA)
{
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);


if(tempPerson
!= null)
{
Console.WriteLine(tempPerson.FirstName);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Person
{
private string lastName = string.Empty;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

private string firstName = string.Empty;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}

public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
}

rodchar said:
hey all,

i have 2 List<T> collections as noted in the subject (call it ListA and
ListB).
i need to iterate thru ListA and for each object in the collection i
need
to
find one of the columns in ListA inside ListB, and if it's found return
that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the
syntax.

thanks,
rodchar
 
R

rodchar

b,

thanks for the help and extra insight,
-rod.

b said:
Ahh...That's a lamda expression. It's not available to you. You'll have to
create a delegate, instead.

Replace "Person tempPerson = listB.Find(x => x.FirstName ==
person.FirstName)"

with

"Person tempPerson = listB.Find(delegate(Person p) {return p.FirstName ==
person.FirstName; })"

rodchar said:
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

what is x and how is it defined? i'm on fx2.0.

b said:
rodchar,

Sorry, but I'm probably too busy to be answering this questions so
quickly.
Here it is with supporting code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Program
{
private static List<Person> listA = new List<Person>();
private static List<Person> listB = new List<Person>();

static void Main(string[] args)
{
listA.Add(new Person("Steve", "Jobs"));
listA.Add(new Person("Bill", "Gates"));
listA.Add(new Person("Dean", "Lee"));
listA.Add(new Person("Jeff", "Abernathy"));

listB.Add(new Person("Steve", "James"));
listB.Add(new Person("Bill", "Rose"));
listB.Add(new Person("Jeff", "Abernathy"));

foreach(Person person in listA)
{
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);


if(tempPerson
!= null)
{
Console.WriteLine(tempPerson.FirstName);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Person
{
private string lastName = string.Empty;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

private string firstName = string.Empty;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}

public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
}

hey all,

i have 2 List<T> collections as noted in the subject (call it ListA and
ListB).
i need to iterate thru ListA and for each object in the collection i
need
to
find one of the columns in ListA inside ListB, and if it's found return
that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the
syntax.

thanks,
rodchar
 
B

b

Anytime...

rodchar said:
b,

thanks for the help and extra insight,
-rod.

b said:
Ahh...That's a lamda expression. It's not available to you. You'll have
to
create a delegate, instead.

Replace "Person tempPerson = listB.Find(x => x.FirstName ==
person.FirstName)"

with

"Person tempPerson = listB.Find(delegate(Person p) {return p.FirstName ==
person.FirstName; })"

rodchar said:
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);

what is x and how is it defined? i'm on fx2.0.

:

rodchar,

Sorry, but I'm probably too busy to be answering this questions so
quickly.
Here it is with supporting code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Program
{
private static List<Person> listA = new List<Person>();
private static List<Person> listB = new List<Person>();

static void Main(string[] args)
{
listA.Add(new Person("Steve", "Jobs"));
listA.Add(new Person("Bill", "Gates"));
listA.Add(new Person("Dean", "Lee"));
listA.Add(new Person("Jeff", "Abernathy"));

listB.Add(new Person("Steve", "James"));
listB.Add(new Person("Bill", "Rose"));
listB.Add(new Person("Jeff", "Abernathy"));

foreach(Person person in listA)
{
Person tempPerson = listB.Find(x => x.FirstName == person.FirstName);


if(tempPerson
!= null)
{
Console.WriteLine(tempPerson.FirstName);
}
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FindTest
{
class Person
{
private string lastName = string.Empty;
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

private string firstName = string.Empty;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}

public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
}

hey all,

i have 2 List<T> collections as noted in the subject (call it ListA
and
ListB).
i need to iterate thru ListA and for each object in the collection i
need
to
find one of the columns in ListA inside ListB, and if it's found
return
that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the
syntax.

thanks,
rodchar
 
G

Göran Andersson

rodchar said:
hey all,

i have 2 List<T> collections as noted in the subject (call it ListA and
ListB).
i need to iterate thru ListA and for each object in the collection i need to
find one of the columns in ListA inside ListB, and if it's found return that
row in ListB, modify it and move on.

is this possible? i noticed List<T>.Find but i'm fumbling with the syntax.

thanks,
rodchar

Searching through a list can get quite slow. If you for example have 100
items in each list, you will be looping through up to 10000 items before
you are done.

First put the items from ListB in a dictionary with the property as key
so that you quickly can find the items. This way you will only be
looping through each collection once, instead of looping through ListB
over and and over again. If you have 100 items in each list, this is up
to 50 times faster.

Example:

Dictionary<string,T> dict = new Dictionary<string,T>();
foreach (T item in ListB) {
dict.Add(item.Property, item);
}
foreach (T item in ListA) {
T found;
if (dict.TryGetValue(item.Property) out found) {
found.SomeOtherProperty = true;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top