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 に関係なく、返される人のリストで、その人に関連付けられているペットが存在するにもかかわらず、ペットが null であることです。

編集:

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

PersonおよびPetがモデルのエンティティであり、if エンティティへPerson.Petsのナビゲーション プロパティであり、すべての完全なエンティティを含む完全なエンティティを持ち、コメントを参照する場合...PetPersonPet

私のメソッドは、名前がsearch.PersonNameとすべてのペットに等しい人のリストを返すことになっていますが、検索でそれらの名前を持つペットを所有している人のみが返されます.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

あなたはこのようなことを試すことができますか

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 に答える
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 に答える