c# - 列出内部 IQueryable 对象

标签 c# linq entity-framework list iqueryable

鉴于以下情况:

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

最佳答案

如果 PersonPet 是您的模型的实体并且如果 Person.PetsPet 实体的导航属性,如果您希望拥有完整的 Person 实体以及所有完整的 Pet 实体并引用您的评论...

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

...你可以使用这个:

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

关于c# - 列出内部 IQueryable 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009681/

相关文章:

c# - 数据访问层的静态方法与依赖注入(inject)

c# - 引导?值 ToString ("N") 不工作

c# - 如何从 Linq 查询返回匿名字段

c# - 如何加 express 归搜索功能?

c# - 从 Entity Framework 调用复杂的存储过程?

c# - Entity Framework 4 映射到 POCO

C#后台 worker ,winform。 "Cross-thread operation not valid:"

c# - 程序运行时创建文件后目录被锁定

c# - 如何使用 Lambda 表达式编写相同的代码

wpf - EF Code First 绑定(bind)到列表框