2010-06-23 98 views
1

我有一個集合(人),有一個多對多的引用到另一個集合(狗)。暫停你不相信每隻狗能有多於一個人的情況。人只是包含的成員是List<Dog>幫助linq查詢。很多很多

我想選擇所有的人都有一定的財產(在IList中指定),寵物有一定的財產(在IList中指定)。

E.g.我有一個IList(僅用於此查詢)與狗的屬性值。

public enum EnumLikesToBite 
{ 
No, 
Yes, 
Sometimes 
} 

IList <<EnumLikesToBite>> listDogsMayBite = 
{ { EnumLikesToBite.Yes}, 
{ EnumLikesToBite.Sometimes}}; 

然後另一個列表人民財產:

public enum EnumKeepsPetWith 
{ 
Chain, 
String, 
Rubberband 
} 
IList <EnumKeepsPetWith> listPeopleWhoDontRestrainDog = 
{ { EnumKeepsPetWith.String }, 
{ EnumKeepsPetWith.Rubberband}}; 

我如何可以查詢出所有誰都有可能咬不抑制狗狗的人。 像這樣的僞代碼:

Var result = from p in People where p.KeepsPet in listPeopleWhoDontRestrainDog and dog.LikesToBite in listDogsMayBite. 

結果有所有的人。當然,如果我能夠得到所有可能咬人的狗,那將會很棒。

回答

1
List<int> mayBite = new List<int>() 
{ 
    (int) EnumLikesToBite.Yes, 
    (int) EnumLikesToBite.Maybe 
} 

List<int> poorRestraint = new List<int>() 
{ 
    (int) EnumKeepsPetWith.String, 
    (int) EnumKeepsPetWith.RubberBand 
} 

IQueryable<Person> query = 
    from p in db.People 
    where poorRestraint.Contains(p.KeepsPetWith) 
    where p.DogPeople.Any(dp => mayBite.Contains(dp.Dog.DoesBite)) 
    select p; 

var query = 
    from p in db.People 
    where poorRestraint.Contains(p.KeepsPetWith) 
    let bitingDogs = 
    from dp in p.DogPeople 
    let d = dp.Dog 
    where mayBite.Contains(d.DoesBite) 
    where bitingDogs.Any() 
    select new {Person = p, BitingDogs = bitingDogs.ToList()}; 
+0

真棒,你怎麼破解它?你喜歡超人還是什麼? – tim 2010-06-24 04:29:23

0

也許這個代碼將幫助..其中一個可能的解決方案是:

var result = 
     peoples.Where(y => dontRestrainDog.Contains(y.KeepsPetWith) && y.Dogs.Any(x => dogsMayBite.Contains(x.LikesToBite))).ToList(); 
    result.ForEach(y => y.Dogs = y.Dogs.Where(x => dogsMayBite.Contains(x.LikesToBite)).ToList()); 

,你可以看到這裏的一個例子:

class Program 
    { 
     static void Main(string[] args) 
     { 
      IList<EnumLikesToBite> dogsMayBite = new List<EnumLikesToBite> 
                { 
                 { EnumLikesToBite.Yes }, { EnumLikesToBite.Sometimes } 
                }; 
      IList<EnumKeepsPetWith> dontRestrainDog = new List<EnumKeepsPetWith> 
                  { 
                   { EnumKeepsPetWith.String }, { EnumKeepsPetWith.Rubberband } 
                  }; 

      var peoples = new List<People>(); 
      var dogs = new List<Dog>(); 
      Random gen = new Random(2); 

      for(int i = 0; i < 10; i++) 
      { 
       People p = new People 
           { 
            PeopleId = i, 
            KeepsPetWith = (EnumKeepsPetWith) (gen.Next(10)%3), 
            Dogs = new List<Dog>() 
       }; 
       Dog d = new Dog 
          { 
           DogId = i, 
           LikesToBite = (EnumLikesToBite) (gen.Next(10)%3), 
           Peoples = new List<People>() 
          }; 
       peoples.Add(p); 
       dogs.Add(d); 
      } 
      for(int i = 0; i < 10; i++) 
      { 
       for(int j = 0; j < 10; j++) 
       { 
        if (gen.Next(10)>7) 
        { 
         peoples[i].Dogs.Add(dogs[j]); 
        } 
        if (gen.Next(10)>7) 
        { 
         dogs[i].Peoples.Add(peoples[j]); 
        } 
       } 
      } 
      PrintDogs(dogs); 
      PrintPeoples(peoples); 

      var result = 
       peoples.Where(y => dontRestrainDog.Contains(y.KeepsPetWith) && y.Dogs.Any(x => dogsMayBite.Contains(x.LikesToBite))).ToList(); 
      result.ForEach(y => y.Dogs = y.Dogs.Where(x => dogsMayBite.Contains(x.LikesToBite)).ToList()); 

      Console.WriteLine("==================="); 
      PrintPeoples(result); 
      Console.ReadLine(); 
     } 

     private static void PrintPeoples(List<People> peoples) 
     { 
      Console.WriteLine("=Peoples="); 
      foreach (var people in peoples) 
      { 
       Console.WriteLine("Id: {0}", people.PeopleId); 
       Console.WriteLine("KeepsPetWith: {0}", people.KeepsPetWith); 
       Console.WriteLine("Dogs: "); 
       foreach (var dog in people.Dogs) 
       { 
        Console.Write("{0}, ", dog.DogId); 
       } 
       Console.WriteLine(); 
      } 
     } 

     private static void PrintDogs(List<Dog> dogs) 
     { 
      Console.WriteLine("=Dogs="); 
      foreach (var dog in dogs) 
      { 
       Console.WriteLine("Id: {0}", dog.DogId); 
       Console.WriteLine("LikesToBite: {0}", dog.LikesToBite); 
       Console.WriteLine("Peoples: "); 
       foreach (var people in dog.Peoples) 
       { 
        Console.Write("{0}, ", people.PeopleId); 
       } 
       Console.WriteLine(); 
      } 
     } 
    } 

    public class People 
    { 
     public int PeopleId { get; set; } 
     public EnumKeepsPetWith KeepsPetWith { get; set; } 
     public IList<Dog> Dogs { get; set; } 
    } 

    public class Dog 
    { 
     public int DogId { get; set; } 
     public EnumLikesToBite LikesToBite { get; set; } 
     public IList<People> Peoples { get; set; } 
    } 

    public enum EnumLikesToBite 
    { 
     No, 
     Yes, 
     Sometimes 
    } 

    public enum EnumKeepsPetWith 
    { 
     Chain, 
     String, 
     Rubberband 
    }