Bryce
Bryce

Reputation: 714

Populate List Where Contains values (or objects) from another list

I want to populate a list where a value in the object is in another list I've already populated. Please see below:

//Populate first object
List<Object1> listObject1 = da.dbsetObject1.Where(p=>p.ID.Equals(SomeID)).ToList();

//Populate second object - what I want to do but can't figure out
List<Object2> listObject2 = da.dbsetObject2.Where(p=>p.ID.Contains(listObject1.Object1ID)).ToList();

I know contains doesn't work but I want to basically populate listObject2 with all the values that have a matching Object1ID. In a sql table this would be a foreign key relationship.

Upvotes: 0

Views: 2480

Answers (4)

JonC
JonC

Reputation: 978

Try something like this.

//Populate first object
List<Object1> listObject1 = da.dbsetObject1.Where(p => p.ID.Equals(SomeID)).ToList();

//Populate second object - what I want to do but can't figure out
List<Object2> listObject2 = da.dbsetObject2.Where(p => listObject1.Any(q => q.ID == p.foreignID)).ToList();

UPDATE

void SomeMethod(){
      var arr = new Object1[]{
            new Object1{Name="n1",ID=1},
            new Object1{Name="n2",ID=2},
            new Object1{Name="n3",ID=3},
            new Object1{Name="n4",ID=4}
        };

        var arr2 = new Object2[]{
            new Object2{Name="o1", Ref=1},
            new Object2{Name="o2", Ref=2},
            new Object2{Name="o3", Ref=1},
            new Object2{Name="o4", Ref=2},
            new Object2{Name="o5", Ref=5},
            new Object2{Name="o6", Ref=3},
            new Object2{Name="o7", Ref=5}
        };

        List<Object1> listObject1 = arr.Where(p => p.ID == 1 || p.ID == 2).ToList();

        List<Object2> listObject2 = arr2.Where(p =>listObject1.Any(q => p.Ref == q.ID)).ToList();
}

    class Object1
    {
        public string Name;
        public int ID;
    }
    class Object2
    {
        public string Name;
        public int Ref;
    }

Upvotes: 1

Bryce
Bryce

Reputation: 714

I figured out the best approach for what I am trying to do. Here is the code I needed:

var NewObject = da.dbsetObject1.Include("listObject2").ToList();

Then I can use NewObject as a list, and when I need an Object2 value associated with a particular Object1 value I can grab it.

Here are the classes:

public class Object1
{
    [Key]
    public int Object1ID { get; set; }
    .....
    public int Object2ID { get; set; }
    public List<Object2> listObject2 { get; set; }
}

public class Object2
{
    [Key]
    public int Object2ID { get; set; }
.....
public int Object1ID { get; set; }
    public Object2 myObject2 { get; set; }
}

Thanks everyone for the help! I wish a lambda expression had worked it looks a lot cleaner especially once I start adding more includes.

Upvotes: 0

Tilak
Tilak

Reputation: 30718

EDIT

Based on updated information in comments

var lstObject1 = listObject1.Select(item -> item.ObjectId).ToArray();  
List<Object2> listObject2 = da.dbsetObject2.Where(p=> lstObject1.Contains(p.ObjectId)).ToList();

Original

Question is not clear from the code

Assuming, you cant p.ID in List, resulting query is

  List<Object2> listObject2 = da.dbsetObject2.Where(p=> listObject1.Contains(p.ID)).ToList();

Upvotes: 3

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79969

Then join the two lists:

var results = from l2 in listObject2
              join l1 in listObject1 on l2.ID equals l1.Object1ID
              select new{anonymous type}

Upvotes: 4

Related Questions