帮助linq查询。很多很多

时间:2010-06-23 19:42:56

标签: linq

我有一个集合(人),它具有多对多的引用(狗)。暂停你对每只狗不止一个人的怀疑。人们只包含List<Dog>

的成员

我想选择人们拥有某种财产(在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.

结果有所有人。当然,如果我可以让所有可能会在那些人身上咬人的狗变得很棒。

2 个答案:

答案 0 :(得分: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()};

答案 1 :(得分: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
    }