LINQ文查询,一对多的关系,请教!!!急!

shanelewis 2013-01-05 08:08:59
主表有多条符合条件的数据
class和student是1:M的关系
类 class
id claseName grade
1001 三年一班 三年级
1002 三年二班 三年级
1003 四年一班 四年级

类 student
id classId age
1001 1002 8
1002 1002 9
1003 1002 9
查出三年级,年龄大于等于9的学生信息

已从数据库查询出来一个list<class>,无条件查询出来的
现在要进行筛选,查出三年二班,年龄大于等于9的学生信息

要求写成list.where(...)形式,不是写成from p in list...

谢谢各位!!!
...全文
6280 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2013-01-05
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Class
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    class Student
    {
        public int ID { get; set; }
        public int ClassID { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Class> classes = new List<Class>() 
            { 
                new Class() { ID = 0, Name = "二年一班" },
                new Class() { ID = 1, Name = "二年二班" },
                new Class() { ID = 2, Name = "三年一班" },
                new Class() { ID = 3, Name = "三年二班" },
                new Class() { ID = 4, Name = "四年一班" },
                new Class() { ID = 5, Name = "四年二班" }
            };
            List<Student> students = new List<Student>()
            {
                new Student() { ID = 0, ClassID = 1, Age = 7 },
                new Student() { ID = 1, ClassID = 2, Age = 8 },
                new Student() { ID = 2, ClassID = 2, Age = 9 },
                new Student() { ID = 3, ClassID = 3, Age = 9 },
                new Student() { ID = 4, ClassID = 3, Age = 8 },
                new Student() { ID = 5, ClassID = 3, Age = 9 },
                new Student() { ID = 6, ClassID = 3, Age = 10 },
                new Student() { ID = 7, ClassID = 4, Age = 9 },
                new Student() { ID = 8, ClassID = 5, Age = 10 },
                new Student() { ID = 9, ClassID = 5, Age = 10 },
            };
            var query = classes.GroupJoin(students, x => x.ID, x => x.ClassID, (g, s) => new { ClassID = g.ID, ClassName = g.Name, Students = s });
            foreach (var item in query)
            {
                Console.WriteLine("name = {0}, id = {1}, students: {{ {2} }}.", item.ClassName, item.ClassID, string.Join("; ", item.Students.Select(x => string.Format("{{ id = {0}, age = {1} }}", x.ID, x.Age))));
            }

            var query1 = classes.GroupJoin(students, x => x.ID, x => x.ClassID, (g, s) => new { ClassID = g.ID, ClassName = g.Name, Students = s }).SelectMany(x => x.Students, (x, y) => new { x.ClassName, y.Age, y.ID, x.ClassID }).Where(x => x.ClassName == "三年二班" && x.Age >= 9);
            foreach (var item in query1)
            {
                Console.WriteLine("id {0}. classid {1}. age {2}.", item.ID, item.ClassID, item.Age);
            }
        }
    }
}
name = 二年一班, id = 0, students: { }. name = 二年二班, id = 1, students: { { id = 0, age = 7 } }. name = 三年一班, id = 2, students: { { id = 1, age = 8 }; { id = 2, age = 9 } }. name = 三年二班, id = 3, students: { { id = 3, age = 9 }; { id = 4, age = 8 }; { id = 5, age = 9 }; { id = 6, age = 10 } }. name = 四年一班, id = 4, students: { { id = 7, age = 9 } }. name = 四年二班, id = 5, students: { { id = 8, age = 10 }; { id = 9, age = 10 } }. id 3. classid 3. age 9. id 5. classid 3. age 9. id 6. classid 3. age 10. Press any key to continue . . .
claymore1114 2013-01-05
  • 打赏
  • 举报
回复
楼上的可以 之前有一帖一样的 需求看似明了,是不是 还没表达清楚? 楼主所说的 主表 是哪个表? linq 是 to sql 还是 to Entity?
  • 打赏
  • 举报
回复
var ary=listclass.Where(t=>t.grade=="三年级").Select(t=>t.id).ToArray(); liststu=liststu.Where(t=>t.age>=9&&ary.Contains(t.classId)).ToList();
  • 打赏
  • 举报
回复
liststu=liststu.Where(t=>t.age>=9&&t.classId==listclass.First(tt=>tt.claseName=="三年二班").id).ToList();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧