List inside IQueryable object
Considering the following:
public class Person
{
public int ID { get; set;}
public string Name { get; set;}
public IQueryable<Pet> Pets { get;set; }
}
public class Pet
{
public int id { get; set; }
public int OwnerId { get; set; }
public string Name { get; set; }
}
public class SearchCriteria
{
public string PersonName {get; set; }
public List<string> PetNames {get; set;}
}
Implementing selection of all faces with their pets during search using IQueryable
public List<Person> GetWithPets(SearchCriteria search)
{
var people = (from p in context.People
where p.Name == search.PersonName
select new Person{
ID = p.ID,
Name = p.Name,
Pets = (from pt in context.Pets
where pt.OwnerId == p.ID
select new Pet {
id = pt.ID,
OwnerId = pt.OwnerId,
Name = pt.Name
}).AsQueryable
}).AsQueryable();
foreach(var str in search.PetNames)
{
people = people.Where(o=>o.Pets.Any(p=>p.Name == str));
}
return people.ToList();
}
My problem is that no matter who is looking for the name, in the list of people who come back, animals have zero value, even if there are pets associated with that person, where did I go wrong?
EDIT:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public IQueryable<Animal> Pets { get; set; }
}
public class Animal
{
public int id { get; set; }
public int? OwnerId { get; set; }
public string Name { get; set; }
}
public class SearchCriteria
{
public string PersonName { get; set; }
public List<string> PetNames { get; set; }
}
class Program
{
public static List<Person> GetWithPets(SearchCriteria search)
{
using (DatabaseEntities context = new DatabaseEntities())
{
var people = (from p in context.Peoples
where p.Name == search.PersonName
select new Person
{
ID = p.ID,
Name = p.Name,
Pets = (from pt in context.Pets
where pt.OwnerID == p.ID
select new Animal
{
id = pt.ID,
OwnerId = pt.OwnerID,
Name = pt.Name
}).AsQueryable()
}).AsQueryable();
foreach (var str in search.PetNames)
{
people = people.Where(o => o.Pets.Any(p => p.Name == str));
}
return people.ToList();
}
}
source to share
If Person
u Pet
are objects of your model and if Person.Pets
is a navigation property for an object Pet
and if you want to have a complete object Person
with all complete Pet
entities and referring to your comment ...
My method should return a list of people with a name equal to search.PersonName and ALL their pets, but only those people who own pets with those names in search.PetNames
... you can use this:
public List<Person> GetWithPets(SearchCriteria search)
{
var people = from p in context.People.Include("Pets")
where p.Name == search.PersonName
&& p.Pets.Any(pt => search.PetNames.Contains(pt.Name))
select p;
return people.ToList();
}
source to share
Can you try something like this
var people = (from p in context.People
where p.Name == search.PersonName
select new Person{
ID = p.ID,
Name = p.Name,
Pets = (from pt in context.Pets
where pt.OwnerId == p.ID
select new Pet {
id = pt.ID,
OwnerId = pt.OwnerId,
Name = pt.Name
})
}).Include("Pets").AsQueryable();
source to share