score:0
May you could 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();
score:0
Or you could try something like this
foreach(var str in search.PetNames)
{
people.Concat(people.Where(o=>o.Pets.Any(p=>p == str)).Include('Pet'));
}
score:0
Add AsQueryable()
to Pets
collection:
Pets = (from pt in context.Pets
where pt.OwnerId == p.ID
select new Pet {
id = pt.ID,
OwnerId = pt.OwnerId,
Name = pt.Name
}).AsQuerable()
score:2
If Person
and Pet
are entities of your model and if Person.Pets
is a navigation property to the Pet
entity and if you want to have the full Person
entity with all the full Pet
entities and refering to your comment...
my method is supposed to return a list of people that have name equals search.PersonName & ALL their pets but only those people who own the pets with those names in the search.PetNames
...you could 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: stackoverflow.com
Related Query
- List Inside IQueryable Object
- Find object inside collection of List
- C# Linq IQueryable select flattened nested object list
- C# IQueryable - How to query a list inside another list
- Xml parse object inside of list of objects inside object using LINQ
- Select a single object from Entity IQueryable List of object
- Linq code to get the index of an object in an array from an object within a list
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Delete the repeated Item inside a List of object C#
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- How to add object to list (or other collection) that is inside another object?
- Return object that has value inside List property
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- List or Array of String Contain specific word in Html Source Code
- Why is my code returning a list of the same object 4 times?
- Delete a duplicate list found inside an object C#
- c# Linq or code to extract groups from a single list of source data
- Select new object from IQueryable returns part of list in a case
- Pass list of object properties to be used to form a new object inside a LINQ query
- How to add to an object to a list inside a database via API post method
- how to save the List inside the object using linq to xml?
- Create a list from two object lists with linq
- how to check if object already exists in a list
- LINQ: Select where object does not contain items from list
- Return list of specific property of object using linq
- Using LINQ, select list of objects inside another list of objects
- Get index of object in a list using Linq
- Create a list of one object type from a list of another using Linq
- Mapping a list of object models onto another list using linq
- Linq select object from list depending on objects attribute
More Query from same tag
- No Exists method so I want to use AsQueryable for defensive programming
- Analyzing queries by Entity Framework
- Select single random item from list of items
- Dynamically querying 2 entity types and mapping results back into specific entity set
- LINQ to Entities question about orderby and null collections
- LINQ SUM, AVERAGE
- Alternatives to nested Select in Linq
- Allowing ad-hoc expressions over IEnumerable<MyObj>
- Can't figure out how to use Linq to XML to do conditional adding of Nodes?
- get first three elements of jagged array
- Is LINQ's Any method efficient?
- Query on a many to one and then returning the parents
- Change Connection String from Application B for Application A
- LINQ foreach during list creation
- C# Sort Parent/Child list to produce a flat output
- Linq on Custom Class List
- How to convert datatable record into custom Class?
- Check null condition for string date conversion to DateTime inside object
- An expression tree lambda may not contain a null propagating operator
- How can I turn SQL query that joins two columns and groups by count of one column and a column of each joined table into LINQ?
- Mvc login page using linq and entity framework
- Can I filter children rather than nested for loop using LINQ
- Get (join) list where linked to optional List via Linq
- How to create LINQ expression?
- How to use Query to add value in IQueryable?
- Transform a DataTable into Dictionary C#
- Select one node from others with same name with Linq to XML and C#
- MongoDB C# Linq Dynamic Querying Nullable Date
- LINQ - filter child collection
- Extracting table using Htmlagilitypack + LINQ + Lambda