日期:2014-05-20  浏览次数:20746 次

linq里多个where如何链接到一块呀?.
假如 List<int> intList = new List<int>();
intList.add(14);
intList.add(24);
intList.add(34);
intList.add(25);
intList.add(65);
intList.add(77);
intList.add(98);
……//会附加很多不确定的数值,只是为了举例而已。

IList<Student> stuList = 从数据库里得到了所有的Student;

foreach(int i in intList )
{
  stuList.Where(o => o.id == i);//大致就这个意思
}

我想检索出来所有学生id == {14,24,34,25,65,77,98};的学生。怎么做呀。谢谢呀。

------解决方案--------------------
var q=stuList.Where(o=>id.Contains(o.id));
------解决方案--------------------
可以对IList中的相进行查询,查询方法大概是这样的,这是我以前写的测试代码,我把主要的代码都给你贴出来了,既然能查询ILIST,就肯定能实现你的功能
C# code

public class Person
    {
        private int _pid;
        public int Pid
        {
            get { return _pid; }
            set { _pid = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        private string _gender;
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        public Person(){ }

        public Person(int __pid, string __name, int __age, string __gender)
        {
            this._pid = __pid;
            this._name = __name;
            this._age = __age;
            this._gender = __gender;
        }
    }
     List<Person> persons = new List<Person>();
    persons.Add(new Person(1, "aaa", 11,"M"));
    persons.Add(new Person(2, "bbb", 22,"M"));
    persons.Add(new Person(3, "ccc", 33,"M"));
     //参数为 谓词(Predicate)
    //查询所有 年龄>18,性别='M' 的Person对象
     List<Person> srcResultList = persons.FindAll(srcCon);
    Console.WriteLine(srcResultList.Count);
    //构建查询条件
     private static bool srcCon(Person p)
        {
            if (p.Age >18 && p.Gender =="M")
                return true;
            else
                return false;
        }

------解决方案--------------------
哈哈,你好像不是问C#代码,抱歉,我就当自己练习了.
------解决方案--------------------

 var stu= from student in stuList 
 group student by student.Scores.Average() >= 80;
int[] intArray = { 3, 1, 6, 4 }; 

var stu = intArray.Select(n=>new { Id = n, Name = (stuList as List <Student>)[n].Name }); 


参考