4

鉴于以下情况:

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;}
}

在使用 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();
}

我的问题是,无论搜索名称的 foreach 是什么,在返回的人员列表中,即使有与该人关联的宠物,宠物也是空的,我哪里出错了?

编辑:

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();
        }
    }
4

4 回答 4

2

如果 PersonPet是您的模型的实体,如果 是该实体Person.Pets的导航属性,如果您希望拥有包含所有完整实体的完整实体并参考您的评论...PetPersonPet

我的方法应该返回一个名字等于 search.PersonName & ALL 他们的宠物的人的列表,但只有那些在 search.PetNames 中拥有这些名字的宠物的人

...你可以使用这个:

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();
}
于 2012-04-04T11:46:59.427 回答
0

添加AsQueryable()Pets收藏:

Pets = (from pt in context.Pets  
        where pt.OwnerId == p.ID  
        select new Pet {  
            id = pt.ID,  
            OwnerId = pt.OwnerId,  
            Name = pt.Name  
        }).AsQuerable() 
于 2012-04-04T11:31:43.650 回答
0

或者你可以尝试这样的事情

foreach(var str in search.PetNames)
       {
            people.Concat(people.Where(o=>o.Pets.Any(p=>p == str)).Include('Pet'));
       }
于 2012-04-04T11:14:13.960 回答
0

你可以试试这样的

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();
于 2012-04-04T11:02:46.803 回答