0

I have an entity Person and a view model that contains a collection of persons.

public class ViewModel
{
   public string Name { get; set; }
   public string Description { get; set; }
   public int Project { get; set; }
   public ICollection<PersonsViewModel> PersonCollection { get; set; }
}

public class PersonsViewModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

These are my tries:

1e:

ICollection<Person> prsn = new PersonRepository().GetAll().Where(x => vm.PersonCollection.Select(y => y.Id).Contains(x.Id)).ToList();

2e:

ICollection<Person> prsn = (from st in new PersonRepository().GetAll()
                            from qw in cm.PersonCollection
                            where st.Id == qw.Id
                            select st).ToList();

Based on this blog post, 3e:

ICollection<Person> prsn = (from st in new PersonRepository().GetAll()
                            from qw in cm.PersonCollection
                            where st.Id.Equals(qw.Id)
                            select st).ToList();

What i'm trying to do is, select the person entity from the datacontext, based on the person id's from the view model. In all the 3 (i did more tries but i lost count) cases i ended up with the run time error as described in the title.

I also found the same question asked here on SO, but it was kind of hard to compare it with mine as there was no extra code available like model/entity.

Can someone point me into the right direction?

4

1 回答 1

0

You can do like this ,

var Ids=vm.PersonCollection.Select(y => y.Id).ToArray();

ICollection<Person> prsn =
 new PersonRepository().GetAll().Where(x => Ids.Contains(x.Id)).ToList();
于 2013-07-04T09:09:08.587 回答